initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / LayerChromium.h
1 /*
2  * Copyright (C) 2010 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31
32 #ifndef LayerChromium_h
33 #define LayerChromium_h
34
35 #if USE(ACCELERATED_COMPOSITING)
36
37 #include "FloatPoint.h"
38 #include "GraphicsContext.h"
39 #include "PlatformString.h"
40 #include "ProgramBinding.h"
41 #include "RenderSurfaceChromium.h"
42 #include "ShaderChromium.h"
43 #include "TransformationMatrix.h"
44
45 #include <wtf/OwnPtr.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/RefCounted.h>
48 #include <wtf/Vector.h>
49 #include <wtf/text/StringHash.h>
50 #include <wtf/text/WTFString.h>
51
52 namespace WebCore {
53
54 class CCLayerImpl;
55 class CCLayerTreeHost;
56 class GraphicsContext3D;
57
58 class CCLayerDelegate {
59 public:
60     virtual ~CCLayerDelegate() { }
61     virtual bool drawsContent() const = 0;
62     virtual bool preserves3D() const = 0;
63     virtual void paintContents(GraphicsContext&, const IntRect& clip) = 0;
64     virtual void notifySyncRequired() = 0;
65 };
66
67 // Base class for composited layers. Special layer types are derived from
68 // this class.
69 class LayerChromium : public RefCounted<LayerChromium> {
70     friend class LayerTilerChromium;
71 public:
72     static PassRefPtr<LayerChromium> create(CCLayerDelegate* = 0);
73
74     virtual ~LayerChromium();
75
76     const LayerChromium* rootLayer() const;
77     LayerChromium* parent() const;
78     void addChild(PassRefPtr<LayerChromium>);
79     void insertChild(PassRefPtr<LayerChromium>, size_t index);
80     void replaceChild(LayerChromium* reference, PassRefPtr<LayerChromium> newLayer);
81     void removeFromParent();
82     void removeAllChildren();
83     void setChildren(const Vector<RefPtr<LayerChromium> >&);
84     const Vector<RefPtr<LayerChromium> >& children() const { return m_children; }
85
86     void setAnchorPoint(const FloatPoint& anchorPoint) { m_anchorPoint = anchorPoint; setNeedsCommit(); }
87     FloatPoint anchorPoint() const { return m_anchorPoint; }
88
89     void setAnchorPointZ(float anchorPointZ) { m_anchorPointZ = anchorPointZ; setNeedsCommit(); }
90     float anchorPointZ() const { return m_anchorPointZ; }
91
92     void setBackgroundColor(const Color& color) { m_backgroundColor = color; setNeedsCommit(); }
93     Color backgroundColor() const { return m_backgroundColor; }
94
95     void setBounds(const IntSize&);
96     const IntSize& bounds() const { return m_bounds; }
97     virtual IntSize contentBounds() const { return bounds(); }
98
99     void setClearsContext(bool clears) { m_clearsContext = clears; setNeedsCommit(); }
100     bool clearsContext() const { return m_clearsContext; }
101
102     void setFrame(const FloatRect&);
103     FloatRect frame() const { return m_frame; }
104
105     void setHidden(bool hidden) { m_hidden = hidden; setNeedsCommit(); }
106     bool isHidden() const { return m_hidden; }
107
108     void setMasksToBounds(bool masksToBounds) { m_masksToBounds = masksToBounds; }
109     bool masksToBounds() const { return m_masksToBounds; }
110
111     void setName(const String&);
112     const String& name() const { return m_name; }
113
114     void setMaskLayer(LayerChromium* maskLayer) { m_maskLayer = maskLayer; }
115     LayerChromium* maskLayer() const { return m_maskLayer.get(); }
116
117     void setNeedsDisplay(const FloatRect& dirtyRect);
118     void setNeedsDisplay();
119     const FloatRect& dirtyRect() const { return m_dirtyRect; }
120     void resetNeedsDisplay();
121
122     void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
123
124     void setOpacity(float opacity) { m_opacity = opacity; setNeedsCommit(); }
125     float opacity() const { return m_opacity; }
126
127     void setOpaque(bool opaque) { m_opaque = opaque; setNeedsCommit(); }
128     bool opaque() const { return m_opaque; }
129
130     void setPosition(const FloatPoint& position) { m_position = position;  setNeedsCommit(); }
131     FloatPoint position() const { return m_position; }
132
133     void setZPosition(float zPosition) { m_zPosition = zPosition; setNeedsCommit(); }
134     float zPosition() const {  return m_zPosition; }
135
136     void setSublayerTransform(const TransformationMatrix& transform) { m_sublayerTransform = transform; setNeedsCommit(); }
137     const TransformationMatrix& sublayerTransform() const { return m_sublayerTransform; }
138
139     void setTransform(const TransformationMatrix& transform) { m_transform = transform; setNeedsCommit(); }
140     const TransformationMatrix& transform() const { return m_transform; }
141
142     const IntRect& visibleLayerRect() const { return m_visibleLayerRect; }
143     void setVisibleLayerRect(const IntRect& visibleLayerRect) { m_visibleLayerRect = visibleLayerRect; }
144
145     const IntPoint& scrollPosition() const { return m_scrollPosition; }
146     void setScrollPosition(const IntPoint& scrollPosition) { m_scrollPosition = scrollPosition; }
147
148     bool doubleSided() const { return m_doubleSided; }
149     void setDoubleSided(bool doubleSided) { m_doubleSided = doubleSided; setNeedsCommit(); }
150
151     // FIXME: This setting is currently ignored.
152     void setGeometryFlipped(bool flipped) { m_geometryFlipped = flipped; setNeedsCommit(); }
153     bool geometryFlipped() const { return m_geometryFlipped; }
154
155     bool preserves3D() { return m_delegate && m_delegate->preserves3D(); }
156
157     void setUsesLayerScissor(bool usesLayerScissor) { m_usesLayerScissor = usesLayerScissor; }
158     bool usesLayerScissor() const { return m_usesLayerScissor; }
159
160     void setIsRootLayer(bool isRootLayer) { m_isRootLayer = isRootLayer; }
161     bool isRootLayer() const { return m_isRootLayer; }
162
163     virtual void setLayerTreeHost(CCLayerTreeHost*);
164
165     void setDelegate(CCLayerDelegate* delegate) { m_delegate = delegate; }
166
167     void setReplicaLayer(LayerChromium* layer) { m_replicaLayer = layer; }
168     LayerChromium* replicaLayer() { return m_replicaLayer.get(); }
169
170     // These methods typically need to be overwritten by derived classes.
171     virtual bool drawsContent() const { return false; }
172     virtual void paintContentsIfDirty() { }
173     virtual void updateCompositorResources(GraphicsContext3D*) { }
174     virtual void setIsMask(bool) {}
175     virtual void unreserveContentsTexture() { }
176     virtual void bindContentsTexture() { }
177     virtual void protectVisibleTileTextures() { }
178
179     // These exists just for debugging (via drawDebugBorder()).
180     void setBorderColor(const Color&);
181
182     void drawDebugBorder();
183
184     void setBorderWidth(float);
185
186     static void drawTexturedQuad(GraphicsContext3D*, const TransformationMatrix& projectionMatrix, const TransformationMatrix& layerMatrix,
187                                  float width, float height, float opacity, const FloatQuad&,
188                                  int matrixLocation, int alphaLocation, int quadLocation);
189
190     virtual void pushPropertiesTo(CCLayerImpl*);
191
192     typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> BorderProgram;
193
194     int id() const { return m_layerId; }
195
196     void clearRenderSurface() { m_renderSurface.clear(); }
197     RenderSurfaceChromium* renderSurface() const { return m_renderSurface.get(); }
198     void createRenderSurface();
199
200     float drawOpacity() const { return m_drawOpacity; }
201     void setDrawOpacity(float opacity) { m_drawOpacity = opacity; }
202     const IntRect& scissorRect() const { return m_scissorRect; }
203     void setScissorRect(const IntRect& rect) { m_scissorRect = rect; }
204     RenderSurfaceChromium* targetRenderSurface() const { return m_targetRenderSurface; }
205     void setTargetRenderSurface(RenderSurfaceChromium* surface) { m_targetRenderSurface = surface; }
206     const TransformationMatrix& drawTransform() const { return m_drawTransform; }
207     void setDrawTransform(const TransformationMatrix& matrix) { m_drawTransform = matrix; }
208     const TransformationMatrix& screenSpaceTransform() const { return m_screenSpaceTransform; }
209     void setScreenSpaceTransform(const TransformationMatrix& matrix) { m_screenSpaceTransform = matrix; }
210     const IntRect& drawableContentRect() const { return m_drawableContentRect; }
211     void setDrawableContentRect(const IntRect& rect) { m_drawableContentRect = rect; }
212
213     // Returns true if any of the layer's descendants has content to draw.
214     bool descendantDrawsContent();
215
216     CCLayerTreeHost* layerTreeHost() const { return m_layerTreeHost.get(); }
217     void cleanupResourcesRecursive();
218
219 protected:
220     CCLayerDelegate* m_delegate;
221     explicit LayerChromium(CCLayerDelegate*);
222
223     // This is called to clean up resources being held in the same context as
224     // layerRendererContext(). Subclasses should override this method if they
225     // hold context-dependent resources such as textures.
226     virtual void cleanupResources();
227
228     static void toGLMatrix(float*, const TransformationMatrix&);
229
230     FloatRect m_dirtyRect;
231     bool m_contentsDirty;
232
233     RefPtr<LayerChromium> m_maskLayer;
234
235     friend class TreeSynchronizer;
236     friend class CCLayerImpl;
237     // Constructs a CCLayerImpl of the correct runtime type for this LayerChromium type.
238     virtual PassRefPtr<CCLayerImpl> createCCLayerImpl();
239     int m_layerId;
240
241 private:
242     void setNeedsCommit();
243
244     void setParent(LayerChromium*);
245     bool hasAncestor(LayerChromium*) const;
246
247     size_t numChildren() const
248     {
249         return m_children.size();
250     }
251
252     // Returns the index of the child or -1 if not found.
253     int indexOfChild(const LayerChromium*);
254
255     // This should only be called from removeFromParent.
256     void removeChild(LayerChromium*);
257
258     Vector<RefPtr<LayerChromium> > m_children;
259     LayerChromium* m_parent;
260
261     RefPtr<CCLayerTreeHost> m_layerTreeHost;
262
263     // Layer properties.
264     IntSize m_bounds;
265     IntRect m_visibleLayerRect;
266     IntPoint m_scrollPosition;
267     FloatPoint m_position;
268     FloatPoint m_anchorPoint;
269     Color m_backgroundColor;
270     Color m_debugBorderColor;
271     float m_debugBorderWidth;
272     float m_opacity;
273     float m_zPosition;
274     float m_anchorPointZ;
275     bool m_clearsContext;
276     bool m_hidden;
277     bool m_masksToBounds;
278     bool m_opaque;
279     bool m_geometryFlipped;
280     bool m_needsDisplayOnBoundsChange;
281     bool m_doubleSided;
282     bool m_usesLayerScissor;
283     bool m_isRootLayer;
284
285     TransformationMatrix m_transform;
286     TransformationMatrix m_sublayerTransform;
287
288     FloatRect m_frame;
289
290     // Replica layer used for reflections.
291     RefPtr<LayerChromium> m_replicaLayer;
292
293     // Transient properties.
294     OwnPtr<RenderSurfaceChromium> m_renderSurface;
295     float m_drawOpacity;
296     IntRect m_scissorRect;
297     RenderSurfaceChromium* m_targetRenderSurface;
298     TransformationMatrix m_drawTransform;
299     TransformationMatrix m_screenSpaceTransform;
300     IntRect m_drawableContentRect;
301
302     String m_name;
303 };
304
305 void sortLayers(Vector<RefPtr<LayerChromium> >::iterator, Vector<RefPtr<LayerChromium> >::iterator, void*);
306
307 }
308 #endif // USE(ACCELERATED_COMPOSITING)
309
310 #endif