Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/page/ChromeClient.h"
30 #include "core/rendering/RenderLayer.h"
31 #include "platform/graphics/GraphicsLayerClient.h"
32 #include "wtf/HashMap.h"
33
34 namespace WebCore {
35
36 class FixedPositionViewportConstraints;
37 class GraphicsLayer;
38 class RenderEmbeddedObject;
39 class RenderLayerStackingNode;
40 class RenderPart;
41 class RenderVideo;
42 class ScrollingCoordinator;
43 class StickyPositionViewportConstraints;
44
45
46 enum CompositingUpdateType {
47     CompositingUpdateAfterStyleChange,
48     CompositingUpdateAfterLayout,
49     CompositingUpdateOnScroll,
50     CompositingUpdateOnCompositedScroll,
51 };
52
53 // RenderLayerCompositor manages the hierarchy of
54 // composited RenderLayers. It determines which RenderLayers
55 // become compositing, and creates and maintains a hierarchy of
56 // GraphicsLayers based on the RenderLayer painting order.
57 //
58 // There is one RenderLayerCompositor per RenderView.
59
60 class RenderLayerCompositor FINAL : public GraphicsLayerClient {
61     WTF_MAKE_FAST_ALLOCATED;
62 public:
63     explicit RenderLayerCompositor(RenderView*);
64     virtual ~RenderLayerCompositor();
65
66     // Return true if this RenderView is in "compositing mode" (i.e. has one or more
67     // composited RenderLayers)
68     bool inCompositingMode() const { return m_compositing; }
69     // This will make a compositing layer at the root automatically, and hook up to
70     // the native view/window system.
71     void enableCompositingMode(bool enable = true);
72
73     bool inForcedCompositingMode() const { return m_forceCompositingMode; }
74
75     // Returns true if the accelerated compositing is enabled
76     bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
77     bool layerSquashingEnabled() const;
78
79     bool canRender3DTransforms() const;
80
81     // Copy the accelerated compositing related flags from Settings
82     void cacheAcceleratedCompositingFlags();
83
84     // Called when the layer hierarchy needs to be updated (compositing layers have been
85     // created, destroyed or re-parented).
86     void setCompositingLayersNeedRebuild(bool needRebuild = true);
87     bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
88
89     // Updating properties required for determining if compositing is necessary.
90     void updateCompositingRequirementsState();
91     void setNeedsUpdateCompositingRequirementsState() { m_needsUpdateCompositingRequirementsState = true; }
92
93     // Used to indicate that a compositing update will be needed for the next frame that gets drawn.
94     void setNeedsCompositingUpdate(CompositingUpdateType);
95
96     // Main entry point for a full update. As needed, this function will compute compositing requirements,
97     // rebuild the composited layer tree, and/or update all the properties assocaited with each layer of the
98     // composited layer tree.
99     void updateCompositingLayers();
100
101     // Update the compositing dirty bits, based on the compositing-impacting properties of the layer.
102     // (At the moment, it also has some legacy compatibility hacks.)
103     void updateLayerCompositingState(RenderLayer*);
104
105     // Update the geometry for compositing children of compositingAncestor.
106     void updateCompositingDescendantGeometry(RenderLayerStackingNode* compositingAncestor, RenderLayer*, bool compositedChildrenOnly);
107
108     // Whether layer's compositedLayerMapping needs a GraphicsLayer to do clipping by an ancestor (non-stacking-context parent with overflow).
109     bool clippedByAncestor(const RenderLayer*) const;
110     // Whether layer's compositedLayerMapping needs a GraphicsLayer to clip z-order children of the given RenderLayer.
111     bool clipsCompositingDescendants(const RenderLayer*) const;
112
113     // Whether the given layer needs an extra 'contents' layer.
114     bool needsContentsCompositingLayer(const RenderLayer*) const;
115
116     bool supportsFixedRootBackgroundCompositing() const;
117     bool needsFixedRootBackgroundLayer(const RenderLayer*) const;
118     GraphicsLayer* fixedRootBackgroundLayer() const;
119
120     // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
121     // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
122     LayoutRect calculateCompositedBounds(const RenderLayer*, const RenderLayer* ancestorLayer) const;
123
124     // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
125     void repaintOnCompositingChange(RenderLayer*);
126
127     void repaintInCompositedAncestor(RenderLayer*, const LayoutRect&);
128
129     // Notify us that a layer has been added or removed
130     void layerWasAdded(RenderLayer* parent, RenderLayer* child);
131     void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
132
133     // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
134     RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
135
136     // Repaint parts of all composited layers that intersect the given absolute rectangle (or the entire layer if the pointer is null).
137     void repaintCompositedLayers(const IntRect* = 0);
138
139     RenderLayer* rootRenderLayer() const;
140     GraphicsLayer* rootGraphicsLayer() const;
141     GraphicsLayer* scrollLayer() const;
142     GraphicsLayer* containerLayer() const;
143
144     enum RootLayerAttachment {
145         RootLayerUnattached,
146         RootLayerAttachedViaChromeClient,
147         RootLayerAttachedViaEnclosingFrame
148     };
149
150     RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
151     void updateRootLayerAttachment();
152     void updateRootLayerPosition();
153
154     void setIsInWindow(bool);
155
156     void clearMappingForAllRenderLayers();
157
158     // Use by RenderVideo to ask if it should try to use accelerated compositing.
159     bool canAccelerateVideoRendering(RenderVideo*) const;
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     static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
166     // Return true if the layers changed.
167     static bool parentFrameContentLayers(RenderPart*);
168
169     // Update the geometry of the layers used for clipping and scrolling in frames.
170     void frameViewDidChangeLocation(const IntPoint& contentsOffset);
171     void frameViewDidChangeSize();
172     void frameViewDidScroll();
173     void frameViewDidLayout();
174     void frameViewScrollbarsExistenceDidChange();
175     void rootFixedBackgroundsChanged();
176
177     bool scrollingLayerDidChange(RenderLayer*);
178
179     String layerTreeAsText(LayerTreeFlags);
180
181     GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
182     GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
183     GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
184
185     void updateViewportConstraintStatus(RenderLayer*);
186     void removeViewportConstrainedLayer(RenderLayer*);
187
188     void addOutOfFlowPositionedLayer(RenderLayer*);
189     void removeOutOfFlowPositionedLayer(RenderLayer*);
190
191     void resetTrackedRepaintRects();
192     void setTracksRepaints(bool);
193
194     void setNeedsToRecomputeCompositingRequirements() { m_needsToRecomputeCompositingRequirements = true; }
195
196     virtual String debugName(const GraphicsLayer*) OVERRIDE;
197
198 private:
199     class OverlapMap;
200
201     enum CompositingStateTransitionType {
202         NoCompositingStateChange,
203         AllocateOwnCompositedLayerMapping,
204         RemoveOwnCompositedLayerMapping,
205         PutInSquashingLayer,
206         RemoveFromSquashingLayer
207     };
208
209     struct SquashingState {
210         SquashingState()
211             : mostRecentMapping(0)
212             , hasMostRecentMapping(false)
213             , nextSquashedLayerIndex(0) { }
214
215         void updateSquashingStateForNewMapping(CompositedLayerMappingPtr, bool hasNewCompositedLayerMapping, IntPoint newOffsetFromAbsoluteForSquashingCLM);
216
217         // The most recent composited backing that the layer should squash onto if needed.
218         CompositedLayerMappingPtr mostRecentMapping;
219         bool hasMostRecentMapping;
220
221         // Absolute coordinates of the compositedLayerMapping's owning layer. This is used for computing the correct
222         // positions of renderlayers when they paint into the squashing layer.
223         IntPoint offsetFromAbsoluteForSquashingCLM;
224
225         // Counter that tracks what index the next RenderLayer would be if it gets squashed to the current squashing layer.
226         size_t nextSquashedLayerIndex;
227     };
228
229     CompositingStateTransitionType computeCompositedLayerUpdate(const RenderLayer*);
230     // Make updates to the layer based on viewport-constrained properties such as position:fixed. This can in turn affect
231     // compositing.
232     bool updateLayerIfViewportConstrained(RenderLayer*);
233
234     // GraphicsLayerClient implementation
235     virtual void notifyAnimationStarted(const GraphicsLayer*, double, double) OVERRIDE { }
236     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE;
237
238     virtual bool isTrackingRepaints() const OVERRIDE;
239
240     // Whether the given RL needs to paint into its own separate backing (and hence would need its own CompositedLayerMapping).
241     bool needsOwnBacking(const RenderLayer*) const;
242     // Whether the layer could ever be composited.
243     bool canBeComposited(const RenderLayer*) const;
244
245     // Returns all direct reasons that a layer should be composited.
246     CompositingReasons directReasonsForCompositing(const RenderLayer*) const;
247
248     void updateDirectCompositingReasons(RenderLayer*);
249
250     // Returns indirect reasons that a layer should be composited because of something in its subtree.
251     CompositingReasons subtreeReasonsForCompositing(RenderObject*, bool hasCompositedDescendants, bool has3DTransformedDescendants) const;
252
253     // Make or destroy the CompositedLayerMapping for this layer; returns true if the compositedLayerMapping changed.
254     bool allocateOrClearCompositedLayerMapping(RenderLayer*);
255     bool updateSquashingAssignment(RenderLayer*, SquashingState&);
256
257     void clearMappingForRenderLayerIncludingDescendants(RenderLayer*);
258
259     // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
260     void recursiveRepaintLayer(RenderLayer*, const IntRect* = 0);
261
262     void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
263     void addToOverlapMapRecursive(OverlapMap&, RenderLayer*, RenderLayer* ancestorLayer = 0);
264
265     // Forces an update for all frames of frame tree recursively. Used only when the mainFrame compositor is ready to
266     // finish all deferred work.
267     static void finishCompositingUpdateForFrameTree(Frame*);
268
269     // Returns true if any layer's compositing changed
270     void computeCompositingRequirements(RenderLayer* ancestorLayer, RenderLayer*, OverlapMap*, struct CompositingRecursionData&, bool& descendantHas3DTransform, Vector<RenderLayer*>& unclippedDescendants);
271
272     // Defines which RenderLayers will paint into which composited backings, by allocating and destroying CompositedLayerMappings as needed.
273     void assignLayersToBackings(RenderLayer*, bool& layersChanged);
274     void assignLayersToBackingsInternal(RenderLayer*, SquashingState&, bool& layersChanged);
275
276     // Allocates, sets up hierarchy, and sets appropriate properties for the GraphicsLayers that correspond to a given
277     // composited RenderLayer. Does nothing if the given RenderLayer does not have a CompositedLayerMapping.
278     void updateGraphicsLayersMappedToRenderLayer(RenderLayer*);
279
280     // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
281     void rebuildCompositingLayerTree(RenderLayer*, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer, int depth);
282
283     // Recurses down the tree, updating layer geometry only.
284     void updateLayerTreeGeometry(RenderLayer*);
285
286     // Hook compositing layers together
287     void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
288     void removeCompositedChildren(RenderLayer*);
289
290     bool layerHas3DContent(const RenderLayer*) const;
291     bool isRunningAcceleratedTransformAnimation(RenderObject*) const;
292
293     bool hasAnyAdditionalCompositedLayers(const RenderLayer* rootLayer) const;
294
295     void ensureRootLayer();
296     void destroyRootLayer();
297
298     void attachRootLayer(RootLayerAttachment);
299     void detachRootLayer();
300
301     bool isMainFrame() const;
302
303     void updateOverflowControlsLayers();
304
305     void notifyIFramesOfCompositingChange();
306
307     Page* page() const;
308
309     GraphicsLayerFactory* graphicsLayerFactory() const;
310     ScrollingCoordinator* scrollingCoordinator() const;
311
312     // Whether a running transition or animation enforces the need for a compositing layer.
313     bool requiresCompositingForAnimation(RenderObject*) const;
314     // Whether a (not necessarily running) transition enforces the need for a compositing layer.
315     bool requiresCompositingForTransition(RenderObject*) const;
316     bool requiresCompositingForTransform(RenderObject*) const;
317     bool requiresCompositingForVideo(RenderObject*) const;
318     bool requiresCompositingForCanvas(RenderObject*) const;
319     bool requiresCompositingForPlugin(RenderObject*) const;
320     bool requiresCompositingForFrame(RenderObject*) const;
321     bool requiresCompositingForBackfaceVisibilityHidden(RenderObject*) const;
322     bool requiresCompositingForFilters(RenderObject*) const;
323     bool requiresCompositingForOverflowScrollingParent(const RenderLayer*) const;
324     bool requiresCompositingForOutOfFlowClipping(const RenderLayer*) const;
325     bool requiresCompositingForScrollableFrame() const;
326     bool requiresCompositingForPosition(RenderObject*, const RenderLayer*, RenderLayer::ViewportConstrainedNotCompositedReason* = 0) const;
327     bool requiresCompositingForOverflowScrolling(const RenderLayer*) const;
328
329     void addViewportConstrainedLayer(RenderLayer*);
330
331     FixedPositionViewportConstraints computeFixedViewportConstraints(RenderLayer*) const;
332     StickyPositionViewportConstraints computeStickyViewportConstraints(RenderLayer*) const;
333
334     bool requiresHorizontalScrollbarLayer() const;
335     bool requiresVerticalScrollbarLayer() const;
336     bool requiresScrollCornerLayer() const;
337 #if USE(RUBBER_BANDING)
338     bool requiresOverhangLayers() const;
339 #endif
340
341     void applyUpdateLayerCompositingStateChickenEggHacks(RenderLayer*, CompositingStateTransitionType compositedLayerUpdate);
342     void assignLayersToBackingsForReflectionLayer(RenderLayer* reflectionLayer, bool& layersChanged);
343
344 private:
345     DocumentLifecycle& lifecycle() const;
346
347     RenderView* m_renderView;
348     OwnPtr<GraphicsLayer> m_rootContentLayer;
349
350     bool m_hasAcceleratedCompositing;
351     ChromeClient::CompositingTriggerFlags m_compositingTriggers;
352
353     bool m_showRepaintCounter;
354
355     // FIXME: This should absolutely not be mutable.
356     mutable bool m_needsToRecomputeCompositingRequirements;
357     bool m_needsToUpdateLayerTreeGeometry;
358
359     bool m_compositing;
360     bool m_compositingLayersNeedRebuild;
361     bool m_forceCompositingMode;
362     bool m_inPostLayoutUpdate; // true when it's OK to trust layout information (e.g. layer sizes and positions)
363     bool m_needsUpdateCompositingRequirementsState;
364
365     bool m_isTrackingRepaints; // Used for testing.
366
367     RootLayerAttachment m_rootLayerAttachment;
368
369     // Enclosing container layer, which clips for iframe content
370     OwnPtr<GraphicsLayer> m_containerLayer;
371     OwnPtr<GraphicsLayer> m_scrollLayer;
372
373     HashSet<RenderLayer*> m_viewportConstrainedLayers;
374     HashSet<RenderLayer*> m_viewportConstrainedLayersNeedingUpdate;
375
376     // This is used in updateCompositingRequirementsState to avoid full tree
377     // walks while determining if layers have unclipped descendants.
378     HashSet<RenderLayer*> m_outOfFlowPositionedLayers;
379
380     // Enclosing layer for overflow controls and the clipping layer
381     OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
382
383     // Layers for overflow controls
384     OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
385     OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
386     OwnPtr<GraphicsLayer> m_layerForScrollCorner;
387 #if USE(RUBBER_BANDING)
388     OwnPtr<GraphicsLayer> m_layerForOverhangShadow;
389 #endif
390 };
391
392
393 } // namespace WebCore
394
395 #endif // RenderLayerCompositor_h