• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GraphicsLayerChromium.h"
40 #include "PlatformString.h"
41 #include "ProgramBinding.h"
42 #include "RenderSurfaceChromium.h"
43 #include "ShaderChromium.h"
44 #include "TransformationMatrix.h"
45 
46 #include <wtf/OwnPtr.h>
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/RefCounted.h>
49 #include <wtf/Vector.h>
50 #include <wtf/text/StringHash.h>
51 #include <wtf/text/WTFString.h>
52 
53 namespace WebCore {
54 
55 class CCLayerImpl;
56 class GraphicsContext3D;
57 class LayerRendererChromium;
58 
59 // Base class for composited layers. Special layer types are derived from
60 // this class.
61 class LayerChromium : public RefCounted<LayerChromium> {
62     friend class LayerTilerChromium;
63 public:
64     static PassRefPtr<LayerChromium> create(GraphicsLayerChromium* owner = 0);
65 
66     virtual ~LayerChromium();
67 
68     const LayerChromium* rootLayer() const;
69     LayerChromium* superlayer() const;
70     void addSublayer(PassRefPtr<LayerChromium>);
71     void insertSublayer(PassRefPtr<LayerChromium>, size_t index);
72     void replaceSublayer(LayerChromium* reference, PassRefPtr<LayerChromium> newLayer);
73     void removeFromSuperlayer();
74     void removeAllSublayers();
75     void setSublayers(const Vector<RefPtr<LayerChromium> >&);
getSublayers()76     const Vector<RefPtr<LayerChromium> >& getSublayers() const { return m_sublayers; }
77 
setAnchorPoint(const FloatPoint & anchorPoint)78     void setAnchorPoint(const FloatPoint& anchorPoint) { m_anchorPoint = anchorPoint; setNeedsCommit(); }
anchorPoint()79     FloatPoint anchorPoint() const { return m_anchorPoint; }
80 
setAnchorPointZ(float anchorPointZ)81     void setAnchorPointZ(float anchorPointZ) { m_anchorPointZ = anchorPointZ; setNeedsCommit(); }
anchorPointZ()82     float anchorPointZ() const { return m_anchorPointZ; }
83 
setBackgroundColor(const Color & color)84     void setBackgroundColor(const Color& color) { m_backgroundColor = color; setNeedsCommit(); }
backgroundColor()85     Color backgroundColor() const { return m_backgroundColor; }
86 
87     void setBounds(const IntSize&);
bounds()88     const IntSize& bounds() const { return m_bounds; }
89 
setClearsContext(bool clears)90     void setClearsContext(bool clears) { m_clearsContext = clears; setNeedsCommit(); }
clearsContext()91     bool clearsContext() const { return m_clearsContext; }
92 
93     void setFrame(const FloatRect&);
frame()94     FloatRect frame() const { return m_frame; }
95 
setHidden(bool hidden)96     void setHidden(bool hidden) { m_hidden = hidden; setNeedsCommit(); }
isHidden()97     bool isHidden() const { return m_hidden; }
98 
setMasksToBounds(bool masksToBounds)99     void setMasksToBounds(bool masksToBounds) { m_masksToBounds = masksToBounds; }
masksToBounds()100     bool masksToBounds() const { return m_masksToBounds; }
101 
102     void setName(const String&);
name()103     const String& name() const { return m_name; }
104 
setMaskLayer(LayerChromium * maskLayer)105     void setMaskLayer(LayerChromium* maskLayer) { m_maskLayer = maskLayer; }
maskDrawLayer()106     CCLayerImpl* maskDrawLayer() const { return m_maskLayer ? m_maskLayer->ccLayerImpl() : 0; }
maskLayer()107     LayerChromium* maskLayer() const { return m_maskLayer.get(); }
108 
109     void setNeedsDisplay(const FloatRect& dirtyRect);
110     void setNeedsDisplay();
invalidateRect(const FloatRect & dirtyRect)111     virtual void invalidateRect(const FloatRect& dirtyRect) {}
dirtyRect()112     const FloatRect& dirtyRect() const { return m_dirtyRect; }
113     void resetNeedsDisplay();
114 
setNeedsDisplayOnBoundsChange(bool needsDisplay)115     void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
116 
setOpacity(float opacity)117     void setOpacity(float opacity) { m_opacity = opacity; setNeedsCommit(); }
opacity()118     float opacity() const { return m_opacity; }
119 
setOpaque(bool opaque)120     void setOpaque(bool opaque) { m_opaque = opaque; setNeedsCommit(); }
opaque()121     bool opaque() const { return m_opaque; }
122 
setPosition(const FloatPoint & position)123     void setPosition(const FloatPoint& position) { m_position = position;  setNeedsCommit(); }
position()124     FloatPoint position() const { return m_position; }
125 
setZPosition(float zPosition)126     void setZPosition(float zPosition) { m_zPosition = zPosition; setNeedsCommit(); }
zPosition()127     float zPosition() const {  return m_zPosition; }
128 
setSublayerTransform(const TransformationMatrix & transform)129     void setSublayerTransform(const TransformationMatrix& transform) { m_sublayerTransform = transform; setNeedsCommit(); }
sublayerTransform()130     const TransformationMatrix& sublayerTransform() const { return m_sublayerTransform; }
131 
setTransform(const TransformationMatrix & transform)132     void setTransform(const TransformationMatrix& transform) { m_transform = transform; setNeedsCommit(); }
transform()133     const TransformationMatrix& transform() const { return m_transform; }
134 
doubleSided()135     bool doubleSided() const { return m_doubleSided; }
setDoubleSided(bool doubleSided)136     void setDoubleSided(bool doubleSided) { m_doubleSided = doubleSided; setNeedsCommit(); }
137 
138     // FIXME: This setting is currently ignored.
setGeometryFlipped(bool flipped)139     void setGeometryFlipped(bool flipped) { m_geometryFlipped = flipped; setNeedsCommit(); }
geometryFlipped()140     bool geometryFlipped() const { return m_geometryFlipped; }
141 
preserves3D()142     bool preserves3D() { return m_owner && m_owner->preserves3D(); }
143 
144     // Derived types must override this method if they need to react to a change
145     // in the LayerRendererChromium.
146     virtual void setLayerRenderer(LayerRendererChromium*);
147 
setOwner(GraphicsLayerChromium * owner)148     void setOwner(GraphicsLayerChromium* owner) { m_owner = owner; }
149 
setReplicaLayer(LayerChromium * layer)150     void setReplicaLayer(LayerChromium* layer) { m_replicaLayer = layer; }
replicaLayer()151     LayerChromium* replicaLayer() { return m_replicaLayer; }
152 
153     // These methods typically need to be overwritten by derived classes.
drawsContent()154     virtual bool drawsContent() const { return false; }
paintContentsIfDirty(const IntRect &)155     virtual void paintContentsIfDirty(const IntRect&) { }
paintContentsIfDirty()156     virtual void paintContentsIfDirty() { }
updateCompositorResources()157     virtual void updateCompositorResources() { }
setIsMask(bool)158     virtual void setIsMask(bool) {}
unreserveContentsTexture()159     virtual void unreserveContentsTexture() { }
bindContentsTexture()160     virtual void bindContentsTexture() { }
draw(const IntRect &)161     virtual void draw(const IntRect&) { }
162 
163     // These exists just for debugging (via drawDebugBorder()).
164     void setBorderColor(const Color&);
165 
166 #ifndef NDEBUG
debugID()167     int debugID() const { return m_debugID; }
168 #endif
169 
170     void drawDebugBorder();
171     String layerTreeAsText() const;
172 
173     void setBorderWidth(float);
174 
175     // Everything from here down in the public section will move to CCLayerImpl.
176     CCLayerImpl* ccLayerImpl();
177     void createCCLayerImplIfNeeded();
178 
179     static void drawTexturedQuad(GraphicsContext3D*, const TransformationMatrix& projectionMatrix, const TransformationMatrix& layerMatrix,
180                                  float width, float height, float opacity,
181                                  int matrixLocation, int alphaLocation);
182 
183     virtual void pushPropertiesTo(CCLayerImpl*);
184 
185     // Begin calls that forward to the CCLayerImpl.
186     LayerRendererChromium* layerRenderer() const;
187     // End calls that forward to the CCLayerImpl.
188 
189     typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> BorderProgram;
190 protected:
191     GraphicsLayerChromium* m_owner;
192     explicit LayerChromium(GraphicsLayerChromium* owner);
193 
194     // This is called to clean up resources being held in the same context as
195     // layerRendererContext(). Subclasses should override this method if they
196     // hold context-dependent resources such as textures.
197     virtual void cleanupResources();
198 
199     GraphicsContext3D* layerRendererContext() const;
200 
201     static void toGLMatrix(float*, const TransformationMatrix&);
202 
203     void dumpLayer(TextStream&, int indent) const;
204 
layerTypeAsString()205     virtual const char* layerTypeAsString() const { return "LayerChromium"; }
206     virtual void dumpLayerProperties(TextStream&, int indent) const;
207 
208     FloatRect m_dirtyRect;
209     bool m_contentsDirty;
210 
211     RefPtr<LayerChromium> m_maskLayer;
212 
213     // All layer shaders share the same attribute locations for the vertex positions
214     // and texture coordinates. This allows switching shaders without rebinding attribute
215     // arrays.
216     static const unsigned s_positionAttribLocation;
217     static const unsigned s_texCoordAttribLocation;
218 
219     // Constructs a CCLayerImpl of the correct runtime type for this LayerChromium type.
220     virtual PassRefPtr<CCLayerImpl> createCCLayerImpl();
221 
222     // For now, the LayerChromium directly owns its CCLayerImpl.
223     RefPtr<CCLayerImpl> m_ccLayerImpl;
224 private:
225     void setNeedsCommit();
226 
setSuperlayer(LayerChromium * superlayer)227     void setSuperlayer(LayerChromium* superlayer) { m_superlayer = superlayer; }
228 
numSublayers()229     size_t numSublayers() const
230     {
231         return m_sublayers.size();
232     }
233 
234     // Returns the index of the sublayer or -1 if not found.
235     int indexOfSublayer(const LayerChromium*);
236 
237     // This should only be called from removeFromSuperlayer.
238     void removeSublayer(LayerChromium*);
239 
240     Vector<RefPtr<LayerChromium> > m_sublayers;
241     LayerChromium* m_superlayer;
242 
243     RefPtr<LayerRendererChromium> m_layerRenderer;
244 
245 #ifndef NDEBUG
246     int m_debugID;
247 #endif
248 
249     // Layer properties.
250     IntSize m_bounds;
251     FloatPoint m_position;
252     FloatPoint m_anchorPoint;
253     Color m_backgroundColor;
254     Color m_debugBorderColor;
255     float m_debugBorderWidth;
256     float m_opacity;
257     float m_zPosition;
258     float m_anchorPointZ;
259     bool m_clearsContext;
260     bool m_hidden;
261     bool m_masksToBounds;
262     bool m_opaque;
263     bool m_geometryFlipped;
264     bool m_needsDisplayOnBoundsChange;
265     bool m_doubleSided;
266 
267     TransformationMatrix m_transform;
268     TransformationMatrix m_sublayerTransform;
269 
270     FloatRect m_frame;
271 
272     // Replica layer used for reflections.
273     LayerChromium* m_replicaLayer;
274 
275     String m_name;
276 };
277 
278 }
279 #endif // USE(ACCELERATED_COMPOSITING)
280 
281 #endif
282