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 RenderLayerBacking_h
27 #define RenderLayerBacking_h
28 
29 #if USE(ACCELERATED_COMPOSITING)
30 
31 #include "FloatPoint.h"
32 #include "FloatPoint3D.h"
33 #include "GraphicsLayer.h"
34 #include "GraphicsLayerClient.h"
35 #include "RenderLayer.h"
36 #include "TransformationMatrix.h"
37 
38 namespace WebCore {
39 
40 class KeyframeList;
41 class RenderLayerCompositor;
42 
43 enum CompositingLayerType {
44     NormalCompositingLayer, // non-tiled layer with backing store
45     TiledCompositingLayer, // tiled layer (always has backing store)
46     MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
47     ContainerCompositingLayer // layer with no backing store
48 };
49 
50 // RenderLayerBacking controls the compositing behavior for a single RenderLayer.
51 // It holds the various GraphicsLayers, and makes decisions about intra-layer rendering
52 // optimizations.
53 //
54 // There is one RenderLayerBacking for each RenderLayer that is composited.
55 
56 class RenderLayerBacking : public GraphicsLayerClient {
57     WTF_MAKE_NONCOPYABLE(RenderLayerBacking); WTF_MAKE_FAST_ALLOCATED;
58 public:
59     RenderLayerBacking(RenderLayer*);
60     ~RenderLayerBacking();
61 
owningLayer()62     RenderLayer* owningLayer() const { return m_owningLayer; }
63 
64     enum UpdateDepth { CompositingChildren, AllDescendants };
65     void updateAfterLayout(UpdateDepth, bool isUpdateRoot);
66 
67     // Returns true if layer configuration changed.
68     bool updateGraphicsLayerConfiguration();
69     // Update graphics layer position and bounds.
70     void updateGraphicsLayerGeometry(); // make private
71     // Update contents and clipping structure.
72     void updateDrawsContent();
73 
graphicsLayer()74     GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); }
75 
76     // Layer to clip children
hasClippingLayer()77     bool hasClippingLayer() const { return m_clippingLayer != 0; }
clippingLayer()78     GraphicsLayer* clippingLayer() const { return m_clippingLayer.get(); }
79 
80     // Layer to get clipped by ancestor
hasAncestorClippingLayer()81     bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != 0; }
ancestorClippingLayer()82     GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
83 
hasContentsLayer()84     bool hasContentsLayer() const { return m_foregroundLayer != 0; }
foregroundLayer()85     GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
86 
hasMaskLayer()87     bool hasMaskLayer() const { return m_maskLayer != 0; }
88 
parentForSublayers()89     GraphicsLayer* parentForSublayers() const { return m_clippingLayer ? m_clippingLayer.get() : m_graphicsLayer.get(); }
childForSuperlayers()90     GraphicsLayer* childForSuperlayers() const { return m_ancestorClippingLayer ? m_ancestorClippingLayer.get() : m_graphicsLayer.get(); }
91 
92     // RenderLayers with backing normally short-circuit paintLayer() because
93     // their content is rendered via callbacks from GraphicsLayer. However, the document
94     // layer is special, because it has a GraphicsLayer to act as a container for the GraphicsLayers
95     // for descendants, but its contents usually render into the window (in which case this returns true).
96     // This returns false for other layers, and when the document layer actually needs to paint into its backing store
97     // for some reason.
98     bool paintingGoesToWindow() const;
99 
100     void setContentsNeedDisplay();
101     // r is in the coordinate space of the layer's render object
102     void setContentsNeedDisplayInRect(const IntRect& r);
103 
104     // Notification from the renderer that its content changed.
105     void contentChanged(RenderLayer::ContentChangeType);
106 
107     // Interface to start, finish, suspend and resume animations and transitions
108     bool startTransition(double timeOffset, int property, const RenderStyle* fromStyle, const RenderStyle* toStyle);
109     void transitionPaused(double timeOffset, int property);
110     void transitionFinished(int property);
111 
112     bool startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes);
113     void animationPaused(double timeOffset, const String& name);
114     void animationFinished(const String& name);
115 
116     void suspendAnimations(double time = 0);
117     void resumeAnimations();
118 
119     IntRect compositedBounds() const;
120     void setCompositedBounds(const IntRect&);
121     void updateCompositedBounds();
122 
123     void updateAfterWidgetResize();
124 
125     // GraphicsLayerClient interface
126     virtual void notifyAnimationStarted(const GraphicsLayer*, double startTime);
127     virtual void notifySyncRequired(const GraphicsLayer*);
128 
129     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip);
130 
131     virtual bool showDebugBorders() const;
132     virtual bool showRepaintCounter() const;
133 
134     IntRect contentsBox() const;
135 
136     // For informative purposes only.
137     CompositingLayerType compositingLayerType() const;
138 
139     void updateContentsScale(float);
140 
layerForHorizontalScrollbar()141     GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
layerForVerticalScrollbar()142     GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
layerForScrollCorner()143     GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
144 
145 private:
146     void createGraphicsLayer();
147     void destroyGraphicsLayer();
148 
renderer()149     RenderBoxModelObject* renderer() const { return m_owningLayer->renderer(); }
compositor()150     RenderLayerCompositor* compositor() const { return m_owningLayer->compositor(); }
151 
152     void updateInternalHierarchy();
153     bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
154     bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
155     bool updateForegroundLayer(bool needsForegroundLayer);
156     bool updateMaskLayer(bool needsMaskLayer);
157     bool requiresHorizontalScrollbarLayer() const;
158     bool requiresVerticalScrollbarLayer() const;
159     bool requiresScrollCornerLayer() const;
160 
161     GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
162 
163     IntSize contentOffsetInCompostingLayer() const;
164     // Result is transform origin in pixels.
165     FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
166     // Result is perspective origin in pixels.
167     FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
168 
169     void updateLayerOpacity(const RenderStyle*);
170     void updateLayerTransform(const RenderStyle*);
171 
172     // Return the opacity value that this layer should use for compositing.
173     float compositingOpacity(float rendererOpacity) const;
174 
175     // Returns true if this compositing layer has no visible content.
176     bool isSimpleContainerCompositingLayer() const;
177     // Returns true if this layer has content that needs to be rendered by painting into the backing store.
178     bool containsPaintedContent() const;
179     // Returns true if the RenderLayer just contains an image that we can composite directly.
180     bool isDirectlyCompositedImage() const;
181     void updateImageContents();
182 
183     bool rendererHasBackground() const;
184     const Color rendererBackgroundColor() const;
185 
186     bool hasNonCompositingDescendants() const;
187 
188     void paintIntoLayer(RenderLayer* rootLayer, GraphicsContext*, const IntRect& paintDirtyRect,
189                     PaintBehavior paintBehavior, GraphicsLayerPaintingPhase, RenderObject* paintingRoot);
190 
191     static int graphicsLayerToCSSProperty(AnimatedPropertyID);
192     static AnimatedPropertyID cssToGraphicsLayerProperty(int);
193 
194 #ifndef NDEBUG
195     String nameForLayer() const;
196 #endif
197 
198 private:
199     RenderLayer* m_owningLayer;
200 
201     OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // only used if we are clipped by an ancestor which is not a stacking context
202     OwnPtr<GraphicsLayer> m_graphicsLayer;
203     OwnPtr<GraphicsLayer> m_foregroundLayer;       // only used in cases where we need to draw the foreground separately
204     OwnPtr<GraphicsLayer> m_clippingLayer;         // only used if we have clipping on a stacking context, with compositing children
205     OwnPtr<GraphicsLayer> m_maskLayer;             // only used if we have a mask
206 
207     OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
208     OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
209     OwnPtr<GraphicsLayer> m_layerForScrollCorner;
210 
211     IntRect m_compositedBounds;
212 
213     bool m_artificiallyInflatedBounds;      // bounds had to be made non-zero to make transform-origin work
214 };
215 
216 } // namespace WebCore
217 
218 #endif // USE(ACCELERATED_COMPOSITING)
219 
220 #endif // RenderLayerBacking_h
221