blob: 5f1a178e216cf559127fc105ecaa3784d59aaf39 [file] [log] [blame]
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +00001/*
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"
simon.fraser@apple.com994de4f2009-06-30 19:53:41 +000030#include "RenderLayerBacking.h"
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000031
32namespace WebCore {
33
34#define PROFILE_LAYER_REBUILD 0
35
36class GraphicsLayer;
simon.fraser@apple.com98df11b2009-06-18 18:52:09 +000037#if ENABLE(VIDEO)
38class RenderVideo;
39#endif
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000040
simon.fraser@apple.com762661b2010-01-28 18:07:14 +000041enum CompositingUpdateType {
42 CompositingUpdateAfterLayoutOrStyleChange,
43 CompositingUpdateOnPaitingOrHitTest,
44 CompositingUpdateOnScroll
45};
46
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000047// RenderLayerCompositor manages the hierarchy of
48// composited RenderLayers. It determines which RenderLayers
49// become compositing, and creates and maintains a hierarchy of
50// GraphicsLayers based on the RenderLayer painting order.
51//
52// There is one RenderLayerCompositor per RenderView.
53
54class RenderLayerCompositor {
55public:
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000056 RenderLayerCompositor(RenderView*);
57 ~RenderLayerCompositor();
58
59 // Return true if this RenderView is in "compositing mode" (i.e. has one or more
60 // composited RenderLayers)
61 bool inCompositingMode() const { return m_compositing; }
62 // This will make a compositing layer at the root automatically, and hook up to
63 // the native view/window system.
64 void enableCompositingMode(bool enable = true);
cmarrin@apple.com89072a02009-06-25 22:03:59 +000065
66 // Returns true if the accelerated compositing is enabled
cmarrin@apple.comd2851822009-06-25 22:35:21 +000067 bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
cmarrin@apple.com89072a02009-06-25 22:03:59 +000068
cmarrin@apple.comf2dff332009-11-18 19:23:44 +000069 bool showDebugBorders() const { return m_showDebugBorders; }
70 bool showRepaintCounter() const { return m_showRepaintCounter; }
71
72 // Copy the accelerated compositing related flags from Settings
73 void cacheAcceleratedCompositingFlags();
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000074
simon.fraser@apple.com8717fee2009-07-31 23:03:25 +000075 // Called when the layer hierarchy needs to be updated (compositing layers have been
simon.fraser@apple.com02f40492009-07-07 17:15:16 +000076 // created, destroyed or re-parented).
77 void setCompositingLayersNeedRebuild(bool needRebuild = true);
78 bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000079
simon.fraser@apple.com02f40492009-07-07 17:15:16 +000080 // Controls whether or not to consult geometry when deciding which layers need
81 // to be composited. Defaults to true.
82 void setCompositingConsultsOverlap(bool b) { m_compositingConsultsOverlap = b; }
83 bool compositingConsultsOverlap() const { return m_compositingConsultsOverlap; }
84
simon.fraser@apple.com8717fee2009-07-31 23:03:25 +000085 void scheduleSync();
cmarrin@apple.com20dc4892009-04-03 19:29:22 +000086
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000087 // Rebuild the tree of compositing layers
simon.fraser@apple.com762661b2010-01-28 18:07:14 +000088 void updateCompositingLayers(CompositingUpdateType = CompositingUpdateAfterLayoutOrStyleChange, RenderLayer* updateRoot = 0);
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000089
simon.fraser@apple.com3bc08f12009-04-07 22:06:19 +000090 // Update the compositing state of the given layer. Returns true if that state changed.
91 enum CompositingChangeRepaint { CompositingChangeRepaintNow, CompositingChangeWillRepaintLater };
92 bool updateLayerCompositingState(RenderLayer*, CompositingChangeRepaint = CompositingChangeRepaintNow);
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +000093
simon.fraser@apple.comb2fc99c2009-06-22 22:45:24 +000094 // Update the geometry for compositing children of compositingAncestor.
simon.fraser@apple.com994de4f2009-06-30 19:53:41 +000095 void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
simon.fraser@apple.comb2fc99c2009-06-22 22:45:24 +000096
simon.fraser@apple.com699dc0b2009-04-07 17:45:00 +000097 // Whether layer's backing needs a graphics layer to do clipping by an ancestor (non-stacking-context parent with overflow).
98 bool clippedByAncestor(RenderLayer*) const;
99 // Whether layer's backing needs a graphics layer to clip z-order children of the given layer.
100 bool clipsCompositingDescendants(const RenderLayer*) const;
101
102 // Whether the given layer needs an extra 'contents' layer.
103 bool needsContentsCompositingLayer(const RenderLayer*) const;
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000104 // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
105 // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
simon.fraser@apple.comb2fc99c2009-06-22 22:45:24 +0000106 IntRect calculateCompositedBounds(const RenderLayer* layer, const RenderLayer* ancestorLayer);
simon.fraser@apple.com3bc08f12009-04-07 22:06:19 +0000107
108 // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
109 void repaintOnCompositingChange(RenderLayer*);
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000110
111 // Notify us that a layer has been added or removed
112 void layerWasAdded(RenderLayer* parent, RenderLayer* child);
113 void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
114
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000115 // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
116 RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
117
118 // Repaint parts of all composited layers that intersect the given absolute rectangle.
119 void repaintCompositedLayersAbsoluteRect(const IntRect&);
120
121 RenderLayer* rootRenderLayer() const;
122 GraphicsLayer* rootPlatformLayer() const;
123
simon.fraser@apple.comc152ae62009-02-03 20:14:37 +0000124 void didMoveOnscreen();
125 void willMoveOffscreen();
126
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000127 void updateRootLayerPosition();
simon.fraser@apple.com98df11b2009-06-18 18:52:09 +0000128
simon.fraser@apple.com02f40492009-07-07 17:15:16 +0000129 void didStartAcceleratedAnimation();
130
simon.fraser@apple.com98df11b2009-06-18 18:52:09 +0000131#if ENABLE(VIDEO)
132 // Use by RenderVideo to ask if it should try to use accelerated compositing.
133 bool canAccelerateVideoRendering(RenderVideo*) const;
134#endif
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000135
simon.fraser@apple.com61cf5eb2009-02-13 05:10:12 +0000136 // Walk the tree looking for layers with 3d transforms. Useful in case you need
137 // to know if there is non-affine content, e.g. for drawing into an image.
138 bool has3DContent() const;
139
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000140private:
141 // Whether the given RL needs a compositing layer.
142 bool needsToBeComposited(const RenderLayer*) const;
143 // Whether the layer has an intrinsic need for compositing layer.
144 bool requiresCompositingLayer(const RenderLayer*) const;
145
simon.fraser@apple.comb2fc99c2009-06-22 22:45:24 +0000146 // Make or destroy the backing for this layer; returns true if backing changed.
147 bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
148
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000149 // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
150 void recursiveRepaintLayerRect(RenderLayer* layer, const IntRect& rect);
151
simon.fraser@apple.com02f40492009-07-07 17:15:16 +0000152 typedef HashMap<RenderLayer*, IntRect> OverlapMap;
153 static void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
154 static bool overlapsCompositedLayers(OverlapMap&, const IntRect& layerBounds);
155
156 // Returns true if any layer's compositing changed
157 void computeCompositingRequirements(RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged);
simon.fraser@apple.com90c94a12009-11-30 03:21:55 +0000158
159 // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
160 void rebuildCompositingLayerTree(RenderLayer* layer, const struct CompositingState&, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer);
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000161
simon.fraser@apple.com90c94a12009-11-30 03:21:55 +0000162 // Recurses down the tree, updating layer geometry only.
163 void updateLayerTreeGeometry(RenderLayer*);
164
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000165 // Hook compositing layers together
166 void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
167 void removeCompositedChildren(RenderLayer*);
168
169 void parentInRootLayer(RenderLayer*);
170
simon.fraser@apple.com61cf5eb2009-02-13 05:10:12 +0000171 bool layerHas3DContent(const RenderLayer*) const;
172
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000173 void ensureRootPlatformLayer();
cmarrin@apple.com89072a02009-06-25 22:03:59 +0000174 void destroyRootPlatformLayer();
175
simon.fraser@apple.com6b59a902009-03-17 20:01:31 +0000176 // Whether a running transition or animation enforces the need for a compositing layer.
simon.fraser@apple.com0ac1f062009-06-27 06:22:30 +0000177 bool requiresCompositingForAnimation(RenderObject*) const;
178 bool requiresCompositingForTransform(RenderObject*) const;
simon.fraser@apple.com98df11b2009-06-18 18:52:09 +0000179 bool requiresCompositingForVideo(RenderObject*) const;
cmarrin@apple.com4e7728f2009-08-27 23:55:44 +0000180 bool requiresCompositingForCanvas(RenderObject*) const;
simon.fraser@apple.comd6aff5e2010-01-12 00:15:06 +0000181 bool requiresCompositingForPlugin(RenderObject*) const;
simon.fraser@apple.com9d9e2982009-08-18 03:57:08 +0000182 bool requiresCompositingWhenDescendantsAreCompositing(RenderObject*) const;
simon.fraser@apple.com6b59a902009-03-17 20:01:31 +0000183
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000184private:
185 RenderView* m_renderView;
simon.fraser@apple.coma90a99d2009-08-17 18:29:36 +0000186 OwnPtr<GraphicsLayer> m_rootPlatformLayer;
simon.fraser@apple.com02f40492009-07-07 17:15:16 +0000187 bool m_hasAcceleratedCompositing;
cmarrin@apple.comf2dff332009-11-18 19:23:44 +0000188 bool m_showDebugBorders;
189 bool m_showRepaintCounter;
simon.fraser@apple.com02f40492009-07-07 17:15:16 +0000190 bool m_compositingConsultsOverlap;
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000191 bool m_compositing;
192 bool m_rootLayerAttached;
simon.fraser@apple.com02f40492009-07-07 17:15:16 +0000193 bool m_compositingLayersNeedRebuild;
cmarrin@apple.com89072a02009-06-25 22:03:59 +0000194
simon.fraser@apple.com6d640a02009-01-31 01:47:49 +0000195#if PROFILE_LAYER_REBUILD
196 int m_rootLayerUpdateCount;
197#endif
198};
199
200
201} // namespace WebCore
202
203#endif // RenderLayerCompositor_h