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/rendering/RenderLayer.h"
30#include "core/rendering/compositing/CompositingReasonFinder.h"
31#include "platform/graphics/GraphicsLayerClient.h"
32#include "wtf/HashMap.h"
33
34namespace blink {
35
36class DocumentLifecycle;
37class GraphicsLayer;
38class GraphicsLayerFactory;
39class Page;
40class RenderPart;
41class ScrollingCoordinator;
42
43enum CompositingUpdateType {
44    CompositingUpdateNone,
45    CompositingUpdateAfterGeometryChange,
46    CompositingUpdateAfterCompositingInputChange,
47    CompositingUpdateRebuildTree,
48};
49
50enum CompositingStateTransitionType {
51    NoCompositingStateChange,
52    AllocateOwnCompositedLayerMapping,
53    RemoveOwnCompositedLayerMapping,
54    PutInSquashingLayer,
55    RemoveFromSquashingLayer
56};
57
58// RenderLayerCompositor manages the hierarchy of
59// composited RenderLayers. It determines which RenderLayers
60// become compositing, and creates and maintains a hierarchy of
61// GraphicsLayers based on the RenderLayer painting order.
62//
63// There is one RenderLayerCompositor per RenderView.
64
65class RenderLayerCompositor FINAL : public GraphicsLayerClient {
66    WTF_MAKE_FAST_ALLOCATED;
67public:
68    explicit RenderLayerCompositor(RenderView&);
69    virtual ~RenderLayerCompositor();
70
71    void updateIfNeededRecursive();
72
73    // Return true if this RenderView is in "compositing mode" (i.e. has one or more
74    // composited RenderLayers)
75    bool inCompositingMode() const;
76    // FIXME: Replace all callers with inCompositingMdoe and remove this function.
77    bool staleInCompositingMode() const;
78    // This will make a compositing layer at the root automatically, and hook up to
79    // the native view/window system.
80    void setCompositingModeEnabled(bool);
81
82    // Returns true if the accelerated compositing is enabled
83    bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
84    bool layerSquashingEnabled() const;
85
86    bool preferCompositingToLCDTextEnabled() const;
87
88    bool rootShouldAlwaysComposite() const;
89
90    // Copy the accelerated compositing related flags from Settings
91    void updateAcceleratedCompositingSettings();
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    void didLayout();
97
98    // Whether layer's compositedLayerMapping needs a GraphicsLayer to clip z-order children of the given RenderLayer.
99    bool clipsCompositingDescendants(const RenderLayer*) const;
100
101    // Whether the given layer needs an extra 'contents' layer.
102    bool needsContentsCompositingLayer(const RenderLayer*) const;
103
104    bool supportsFixedRootBackgroundCompositing() const;
105    bool needsFixedRootBackgroundLayer(const RenderLayer*) const;
106    GraphicsLayer* fixedRootBackgroundLayer() const;
107    void setNeedsUpdateFixedBackground() { m_needsUpdateFixedBackground = true; }
108
109    // Issue paint invalidations of the appropriate layers when the given RenderLayer starts or stops being composited.
110    void paintInvalidationOnCompositingChange(RenderLayer*);
111
112    void fullyInvalidatePaint();
113
114    RenderLayer* rootRenderLayer() const;
115    GraphicsLayer* rootGraphicsLayer() const;
116    GraphicsLayer* scrollLayer() const;
117    GraphicsLayer* containerLayer() const;
118
119    // We don't always have a root transform layer. This function lazily allocates one
120    // and returns it as required.
121    GraphicsLayer* ensureRootTransformLayer();
122
123    enum RootLayerAttachment {
124        RootLayerUnattached,
125        RootLayerAttachedViaChromeClient,
126        RootLayerAttachedViaEnclosingFrame
127    };
128
129    RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
130    void updateRootLayerAttachment();
131    void updateRootLayerPosition();
132
133    void setIsInWindow(bool);
134
135    static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
136    // Return true if the layers changed.
137    static bool parentFrameContentLayers(RenderPart*);
138
139    // Update the geometry of the layers used for clipping and scrolling in frames.
140    void frameViewDidChangeLocation(const IntPoint& contentsOffset);
141    void frameViewDidChangeSize();
142    void frameViewDidScroll();
143    void frameViewScrollbarsExistenceDidChange();
144    void rootFixedBackgroundsChanged();
145
146    bool scrollingLayerDidChange(RenderLayer*);
147
148    String layerTreeAsText(LayerTreeFlags);
149
150    GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
151    GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
152    GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
153
154    void resetTrackedPaintInvalidationRects();
155    void setTracksPaintInvalidations(bool);
156
157    virtual String debugName(const GraphicsLayer*) OVERRIDE;
158    DocumentLifecycle& lifecycle() const;
159
160    void updatePotentialCompositingReasonsFromStyle(RenderLayer*);
161
162    // Whether the layer could ever be composited.
163    bool canBeComposited(const RenderLayer*) const;
164
165    // FIXME: Move allocateOrClearCompositedLayerMapping to CompositingLayerAssigner once we've fixed
166    // the compositing chicken/egg issues.
167    bool allocateOrClearCompositedLayerMapping(RenderLayer*, CompositingStateTransitionType compositedLayerUpdate);
168
169    void updateDirectCompositingReasons(RenderLayer*);
170
171    void setOverlayLayer(GraphicsLayer*);
172
173    bool inOverlayFullscreenVideo() const { return m_inOverlayFullscreenVideo; }
174
175private:
176#if ENABLE(ASSERT)
177    void assertNoUnresolvedDirtyBits();
178#endif
179
180    // GraphicsLayerClient implementation
181    virtual void notifyAnimationStarted(const GraphicsLayer*, double) OVERRIDE { }
182    virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE;
183
184    virtual bool isTrackingPaintInvalidations() const OVERRIDE;
185
186    void updateWithoutAcceleratedCompositing(CompositingUpdateType);
187    void updateIfNeeded();
188
189    void ensureRootLayer();
190    void destroyRootLayer();
191
192    void attachRootLayer(RootLayerAttachment);
193    void detachRootLayer();
194
195    void updateOverflowControlsLayers();
196
197    Page* page() const;
198
199    GraphicsLayerFactory* graphicsLayerFactory() const;
200    ScrollingCoordinator* scrollingCoordinator() const;
201
202    void enableCompositingModeIfNeeded();
203
204    bool requiresHorizontalScrollbarLayer() const;
205    bool requiresVerticalScrollbarLayer() const;
206    bool requiresScrollCornerLayer() const;
207
208    void applyOverlayFullscreenVideoAdjustment();
209
210    RenderView& m_renderView;
211    OwnPtr<GraphicsLayer> m_rootContentLayer;
212    OwnPtr<GraphicsLayer> m_rootTransformLayer;
213
214    CompositingReasonFinder m_compositingReasonFinder;
215
216    CompositingUpdateType m_pendingUpdateType;
217
218    bool m_hasAcceleratedCompositing;
219    bool m_compositing;
220
221    // The root layer doesn't composite if it's a non-scrollable frame.
222    // So, after a layout we set this dirty bit to know that we need
223    // to recompute whether the root layer should composite even if
224    // none of its descendants composite.
225    // FIXME: Get rid of all the callers of setCompositingModeEnabled
226    // except the one in updateIfNeeded, then rename this to
227    // m_compositingDirty.
228    bool m_rootShouldAlwaysCompositeDirty;
229    bool m_needsUpdateFixedBackground;
230    bool m_isTrackingPaintInvalidations; // Used for testing.
231
232    RootLayerAttachment m_rootLayerAttachment;
233
234    // Enclosing container layer, which clips for iframe content
235    OwnPtr<GraphicsLayer> m_containerLayer;
236    OwnPtr<GraphicsLayer> m_scrollLayer;
237
238    // Enclosing layer for overflow controls and the clipping layer
239    OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
240
241    // Layers for overflow controls
242    OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
243    OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
244    OwnPtr<GraphicsLayer> m_layerForScrollCorner;
245#if USE(RUBBER_BANDING)
246    OwnPtr<GraphicsLayer> m_layerForOverhangShadow;
247#endif
248
249    bool m_inOverlayFullscreenVideo;
250};
251
252} // namespace blink
253
254#endif // RenderLayerCompositor_h
255