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