a1f39e83617e454fe1a6e829a65207fad0b5135a
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / CompositedLayerMapping.h
1 /*
2  * Copyright (C) 2009, 2010, 2011 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 CompositedLayerMapping_h
27 #define CompositedLayerMapping_h
28
29 #include "core/rendering/RenderLayer.h"
30 #include "platform/geometry/FloatPoint.h"
31 #include "platform/geometry/FloatPoint3D.h"
32 #include "platform/graphics/GraphicsLayer.h"
33 #include "platform/graphics/GraphicsLayerClient.h"
34 #include "platform/transforms/TransformationMatrix.h"
35
36 namespace WebCore {
37
38 class KeyframeList;
39 class RenderLayerCompositor;
40
41 enum CompositingLayerType {
42     NormalCompositingLayer, // non-tiled layer with backing store
43     MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
44     ContainerCompositingLayer // layer with no backing store
45 };
46
47
48 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer.
49 struct GraphicsLayerPaintInfo {
50     RenderLayer* renderLayer;
51
52     LayoutRect compositedBounds;
53
54     // At first, the m_squashingLayer's bounds/location are not known. The value offsetFromSquashingCLM is
55     // an intermediate offset for a squashed RenderLayer, described with respect to the CompositedLayerMapping's
56     // owning layer that would eventually have the m_squashingLayer. Once the shared GraphicsLayer's bounds are
57     // known, then we can trivially convert this offset to m_squashingLayer's space.
58     IntSize offsetFromSquashingCLM;
59
60     // Offset describing where this squashed RenderLayer paints into the shared GraphicsLayer backing.
61     IntSize offsetFromRenderer;
62
63     GraphicsLayerPaintingPhase paintingPhase;
64
65     bool isBackgroundLayer;
66
67     bool isEquivalentForSquashing(const GraphicsLayerPaintInfo& other)
68     {
69         // FIXME: offsetFromRenderer and compositedBounds should not be checked here, because
70         // they are not yet fixed at the time this function is used.
71         return renderLayer == other.renderLayer
72             && offsetFromSquashingCLM == other.offsetFromSquashingCLM
73             && paintingPhase == other.paintingPhase
74             && isBackgroundLayer == other.isBackgroundLayer;
75     }
76 };
77
78 // CompositedLayerMapping keeps track of how RenderLayers of the render tree correspond to
79 // GraphicsLayers of the composited layer tree. Each instance of CompositedLayerMapping
80 // manages a small cluster of GraphicsLayers and the references to which RenderLayers
81 // and paint phases contribute to each GraphicsLayer.
82 //
83 // Currently (Oct. 2013) there is one CompositedLayerMapping for each RenderLayer,
84 // but this is likely to evolve soon.
85 class CompositedLayerMapping FINAL : public GraphicsLayerClient {
86     WTF_MAKE_NONCOPYABLE(CompositedLayerMapping); WTF_MAKE_FAST_ALLOCATED;
87 public:
88     explicit CompositedLayerMapping(RenderLayer*);
89     virtual ~CompositedLayerMapping();
90
91     RenderLayer* owningLayer() const { return m_owningLayer; }
92
93     enum UpdateAfterLayoutFlag {
94         CompositingChildrenOnly = 1 << 0,
95         NeedsFullRepaint = 1 << 1,
96         IsUpdateRoot = 1 << 2
97     };
98     typedef unsigned UpdateAfterLayoutFlags;
99     void updateAfterLayout(UpdateAfterLayoutFlags);
100
101     // Returns true if layer configuration changed.
102     bool updateGraphicsLayerConfiguration();
103     // Update graphics layer position and bounds.
104     void updateGraphicsLayerGeometry(); // make private
105     // Update whether layer needs blending.
106     void updateContentsOpaque();
107
108     GraphicsLayer* mainGraphicsLayer() const { return m_graphicsLayer.get(); }
109
110     // Layer to clip children
111     bool hasClippingLayer() const { return m_childContainmentLayer; }
112     GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); }
113
114     // Layer to get clipped by ancestor
115     bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer; }
116     GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
117
118     bool hasContentsLayer() const { return m_foregroundLayer; }
119     GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
120
121     GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
122     bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
123
124     bool hasScrollingLayer() const { return m_scrollingLayer; }
125     GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
126     GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
127
128     bool hasMaskLayer() const { return m_maskLayer; }
129     GraphicsLayer* maskLayer() const { return m_maskLayer.get(); }
130
131     bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer; }
132     GraphicsLayer* childClippingMaskLayer() const { return m_childClippingMaskLayer.get(); }
133
134     GraphicsLayer* parentForSublayers() const;
135     GraphicsLayer* childForSuperlayers() const;
136     // localRootForOwningLayer does not include the m_squashingContainmentLayer, which is technically not associated with this CLM's owning layer.
137     GraphicsLayer* localRootForOwningLayer() const;
138
139     GraphicsLayer* childTransformLayer() const { return m_childTransformLayer.get(); }
140
141     GraphicsLayer* squashingContainmentLayer() const { return m_squashingContainmentLayer.get(); }
142     GraphicsLayer* squashingLayer() const { return m_squashingLayer.get(); }
143     // Contains the bottommost layer in the hierarchy that can contain the children transform.
144     GraphicsLayer* layerForChildrenTransform() const;
145
146     // Returns true for a composited layer that has no backing store of its own, so
147     // paints into some ancestor layer.
148     bool paintsIntoCompositedAncestor() const { return !(m_requiresOwnBackingStoreForAncestorReasons || m_requiresOwnBackingStoreForIntrinsicReasons); }
149
150     // Updates whether a backing store is needed based on the layer's compositing ancestor's
151     // properties; returns true if the need for a backing store for ancestor reasons changed.
152     bool updateRequiresOwnBackingStoreForAncestorReasons(const RenderLayer* compositingAncestor);
153
154     // Updates whether a backing store is needed for intrinsic reasons (that is, based on the
155     // layer's own properties or compositing reasons); returns true if the intrinsic need for
156     // a backing store changed.
157     bool updateRequiresOwnBackingStoreForIntrinsicReasons();
158
159     void setContentsNeedDisplay();
160     // r is in the coordinate space of the layer's render object
161     void setContentsNeedDisplayInRect(const IntRect&);
162
163     // Notification from the renderer that its content changed.
164     void contentChanged(ContentChangeType);
165
166     LayoutRect compositedBounds() const;
167     void setCompositedBounds(const LayoutRect&);
168     void updateCompositedBounds();
169
170     void updateAfterWidgetResize();
171     void positionOverflowControlsLayers(const IntSize& offsetFromRoot);
172     bool hasUnpositionedOverflowControlsLayers() const;
173
174     // Returns true if the assignment actually changed the assigned squashing layer.
175     bool updateSquashingLayerAssignment(RenderLayer*, IntSize offsetFromSquashingCLM, size_t nextSquashedLayerIndex);
176     void removeRenderLayerFromSquashingGraphicsLayer(const RenderLayer*);
177
178     void finishAccumulatingSquashingLayers(size_t nextSquashedLayerIndex);
179     void updateRenderingContext();
180     void updateShouldFlattenTransform();
181
182     // GraphicsLayerClient interface
183     virtual void notifyAnimationStarted(const GraphicsLayer*, double wallClockTime, double monotonicTime) OVERRIDE;
184     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE;
185     virtual bool isTrackingRepaints() const OVERRIDE;
186
187     PassOwnPtr<Vector<FloatRect> > collectTrackedRepaintRects() const;
188
189 #ifndef NDEBUG
190     virtual void verifyNotPainting() OVERRIDE;
191 #endif
192
193     LayoutRect contentsBox() const;
194     IntRect backgroundBox() const;
195
196     // For informative purposes only.
197     CompositingLayerType compositingLayerType() const;
198
199     GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
200     GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
201     GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
202
203     void updateFilters(const RenderStyle*);
204     bool canCompositeFilters() const { return m_canCompositeFilters; }
205
206     void setBlendMode(blink::WebBlendMode);
207
208     virtual String debugName(const GraphicsLayer*) OVERRIDE;
209
210     LayoutSize subpixelAccumulation() const { return m_subpixelAccumulation; }
211 private:
212     void createPrimaryGraphicsLayer();
213     void destroyGraphicsLayers();
214
215     PassOwnPtr<GraphicsLayer> createGraphicsLayer(CompositingReasons);
216     bool toggleScrollbarLayerIfNeeded(OwnPtr<GraphicsLayer>&, bool needsLayer);
217
218     RenderLayerModelObject* renderer() const { return m_owningLayer->renderer(); }
219     RenderLayerCompositor* compositor() const { return m_owningLayer->compositor(); }
220
221     void updateInternalHierarchy();
222     bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
223     bool updateChildTransformLayer(bool needsChildTransformLayer);
224     bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
225     bool updateForegroundLayer(bool needsForegroundLayer);
226     bool updateBackgroundLayer(bool needsBackgroundLayer);
227     bool updateMaskLayer(bool needsMaskLayer);
228     bool updateClippingMaskLayers(bool needsChildClippingMaskLayer);
229     bool requiresHorizontalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->horizontalScrollbar(); }
230     bool requiresVerticalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->verticalScrollbar(); }
231     bool requiresScrollCornerLayer() const { return m_owningLayer->scrollableArea() && !m_owningLayer->scrollableArea()->scrollCornerAndResizerRect().isEmpty(); }
232     bool updateScrollingLayers(bool scrollingLayers);
233     void updateScrollParent(RenderLayer*);
234     void updateClipParent(RenderLayer*);
235     bool updateSquashingLayers(bool needsSquashingLayers);
236     void updateDrawsContent(bool isSimpleContainer);
237     void updateChildrenTransform();
238     void registerScrollingLayers();
239
240     void adjustBoundsForSubPixelAccumulation(const RenderLayer* compositedAncestor, IntRect& localCompositingBounds, IntRect& relativeCompositingBounds, IntPoint& delta);
241
242     void setBackgroundLayerPaintsFixedRootBackground(bool);
243
244     GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
245
246     LayoutSize contentOffsetInCompostingLayer() const;
247     // Result is transform origin in pixels.
248     FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
249     // Result is perspective origin in pixels.
250     FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
251
252     void updateSquashingLayerGeometry(const IntPoint& delta);
253
254     void updateOpacity(const RenderStyle*);
255     void updateTransform(const RenderStyle*);
256     void updateLayerBlendMode(const RenderStyle*);
257     void updateIsRootForIsolatedGroup();
258     // Return the opacity value that this layer should use for compositing.
259     float compositingOpacity(float rendererOpacity) const;
260
261     bool isMainFrameRenderViewLayer() const;
262
263     bool paintsBoxDecorations() const;
264     bool paintsChildren() const;
265
266     // Returns true if this compositing layer has no visible content.
267     bool isSimpleContainerCompositingLayer() const;
268     // Returns true if this layer has content that needs to be rendered by painting into the backing store.
269     bool containsPaintedContent(bool isSimpleContainer) const;
270     // Returns true if the RenderLayer just contains an image that we can composite directly.
271     bool isDirectlyCompositedImage() const;
272     void updateImageContents();
273
274     Color rendererBackgroundColor() const;
275     void updateBackgroundColor(bool isSimpleContainer);
276     void updateContentsRect(bool isSimpleContainer);
277
278     void updateCompositingReasons();
279
280     bool hasVisibleNonCompositingDescendantLayers() const;
281
282     bool shouldClipCompositedBounds() const;
283
284     void paintsIntoCompositedAncestorChanged();
285
286     void doPaintTask(GraphicsLayerPaintInfo&, GraphicsContext*, const IntRect& clip);
287
288     RenderLayer* m_owningLayer;
289
290     // The hierarchy of layers that is maintained by the CompositedLayerMapping looks like this:
291     //
292     //  + m_ancestorClippingLayer [OPTIONAL]
293     //     + m_graphicsLayer
294     //        + m_childContainmentLayer [OPTIONAL] <-OR-> m_scrollingLayer [OPTIONAL] <-OR-> m_childTransformLayer
295     //                                                     + m_scrollingContentsLayer [OPTIONAL]
296     //
297     // We need an ancestor clipping layer if our clipping ancestor is not our ancestor in the
298     // clipping tree. Here's what that might look like.
299     //
300     // Let A = the clipping ancestor,
301     //     B = the clip descendant, and
302     //     SC = the stacking context that is the ancestor of A and B in the stacking tree.
303     //
304     // SC
305     //  + A = m_graphicsLayer
306     //  |  + m_childContainmentLayer
307     //  |     + ...
308     //  ...
309     //  |
310     //  + B = m_ancestorClippingLayer [+]
311     //     + m_graphicsLayer
312     //        + ...
313     //
314     // In this case B is clipped by another layer that doesn't happen to be its ancestor: A.
315     // So we create an ancestor clipping layer for B, [+], which ensures that B is clipped
316     // as if it had been A's descendant.
317     OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
318     OwnPtr<GraphicsLayer> m_graphicsLayer;
319     OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children.
320     OwnPtr<GraphicsLayer> m_childTransformLayer; // Only used if we have perspective and no m_childContainmentLayer.
321     OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
322     OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
323
324     // This layer is also added to the hierarchy by the RLB, but in a different way than
325     // the layers above. It's added to m_graphicsLayer as its mask layer (naturally) if
326     // we have a mask, and isn't part of the typical hierarchy (it has no children).
327     OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
328     OwnPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
329
330     // There are two other (optional) layers whose painting is managed by the CompositedLayerMapping,
331     // but whose position in the hierarchy is maintained by the RenderLayerCompositor. These
332     // are the foreground and background layers. The foreground layer exists if we have composited
333     // descendants with negative z-order. We need the extra layer in this case because the layer
334     // needs to draw both below (for the background, say) and above (for the normal flow content, say)
335     // the negative z-order descendants and this is impossible with a single layer. The RLC handles
336     // inserting m_foregroundLayer in the correct position in our descendant list for us (right after
337     // the neg z-order dsecendants).
338     //
339     // The background layer is only created if this is the root layer and our background is entirely
340     // fixed. In this case we want to put the background in a separate composited layer so that when
341     // we scroll, we don't have to re-raster the background into position. This layer is also inserted
342     // into the tree by the RLC as it gets a special home. This layer becomes a descendant of the
343     // frame clipping layer. That is:
344     //   ...
345     //     + frame clipping layer
346     //       + m_backgroundLayer
347     //       + frame scrolling layer
348     //         + root content layer
349     //
350     // With the hierarchy set up like this, the root content layer is able to scroll without affecting
351     // the background layer (or repainting).
352     OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
353     OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
354
355     OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
356     OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
357     OwnPtr<GraphicsLayer> m_layerForScrollCorner;
358
359     OwnPtr<GraphicsLayer> m_squashingContainmentLayer; // Only used if any squashed layers exist, to contain the squashed layers as siblings to the rest of the GraphicsLayer tree chunk.
360     OwnPtr<GraphicsLayer> m_squashingLayer; // Only used if any squashed layers exist, this is the backing that squashed layers paint into.
361     Vector<GraphicsLayerPaintInfo> m_squashedLayers;
362
363     LayoutRect m_compositedBounds;
364     LayoutSize m_subpixelAccumulation; // The accumulated subpixel offset of the compositedBounds compared to absolute coordinates.
365
366     bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
367     bool m_isMainFrameRenderViewLayer;
368     bool m_requiresOwnBackingStoreForIntrinsicReasons;
369     bool m_requiresOwnBackingStoreForAncestorReasons;
370     bool m_canCompositeFilters;
371     bool m_backgroundLayerPaintsFixedRootBackground;
372 };
373
374 } // namespace WebCore
375
376 #endif // CompositedLayerMapping_h