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
53namespace WebCore {
54
55class CCLayerImpl;
56class GraphicsContext3D;
57class LayerRendererChromium;
58
59// Base class for composited layers. Special layer types are derived from
60// this class.
61class LayerChromium : public RefCounted<LayerChromium> {
62    friend class LayerTilerChromium;
63public:
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> >&);
76    const Vector<RefPtr<LayerChromium> >& getSublayers() const { return m_sublayers; }
77
78    void setAnchorPoint(const FloatPoint& anchorPoint) { m_anchorPoint = anchorPoint; setNeedsCommit(); }
79    FloatPoint anchorPoint() const { return m_anchorPoint; }
80
81    void setAnchorPointZ(float anchorPointZ) { m_anchorPointZ = anchorPointZ; setNeedsCommit(); }
82    float anchorPointZ() const { return m_anchorPointZ; }
83
84    void setBackgroundColor(const Color& color) { m_backgroundColor = color; setNeedsCommit(); }
85    Color backgroundColor() const { return m_backgroundColor; }
86
87    void setBounds(const IntSize&);
88    const IntSize& bounds() const { return m_bounds; }
89
90    void setClearsContext(bool clears) { m_clearsContext = clears; setNeedsCommit(); }
91    bool clearsContext() const { return m_clearsContext; }
92
93    void setFrame(const FloatRect&);
94    FloatRect frame() const { return m_frame; }
95
96    void setHidden(bool hidden) { m_hidden = hidden; setNeedsCommit(); }
97    bool isHidden() const { return m_hidden; }
98
99    void setMasksToBounds(bool masksToBounds) { m_masksToBounds = masksToBounds; }
100    bool masksToBounds() const { return m_masksToBounds; }
101
102    void setName(const String&);
103    const String& name() const { return m_name; }
104
105    void setMaskLayer(LayerChromium* maskLayer) { m_maskLayer = maskLayer; }
106    CCLayerImpl* maskDrawLayer() const { return m_maskLayer ? m_maskLayer->ccLayerImpl() : 0; }
107    LayerChromium* maskLayer() const { return m_maskLayer.get(); }
108
109    void setNeedsDisplay(const FloatRect& dirtyRect);
110    void setNeedsDisplay();
111    virtual void invalidateRect(const FloatRect& dirtyRect) {}
112    const FloatRect& dirtyRect() const { return m_dirtyRect; }
113    void resetNeedsDisplay();
114
115    void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
116
117    void setOpacity(float opacity) { m_opacity = opacity; setNeedsCommit(); }
118    float opacity() const { return m_opacity; }
119
120    void setOpaque(bool opaque) { m_opaque = opaque; setNeedsCommit(); }
121    bool opaque() const { return m_opaque; }
122
123    void setPosition(const FloatPoint& position) { m_position = position;  setNeedsCommit(); }
124    FloatPoint position() const { return m_position; }
125
126    void setZPosition(float zPosition) { m_zPosition = zPosition; setNeedsCommit(); }
127    float zPosition() const {  return m_zPosition; }
128
129    void setSublayerTransform(const TransformationMatrix& transform) { m_sublayerTransform = transform; setNeedsCommit(); }
130    const TransformationMatrix& sublayerTransform() const { return m_sublayerTransform; }
131
132    void setTransform(const TransformationMatrix& transform) { m_transform = transform; setNeedsCommit(); }
133    const TransformationMatrix& transform() const { return m_transform; }
134
135    bool doubleSided() const { return m_doubleSided; }
136    void setDoubleSided(bool doubleSided) { m_doubleSided = doubleSided; setNeedsCommit(); }
137
138    // FIXME: This setting is currently ignored.
139    void setGeometryFlipped(bool flipped) { m_geometryFlipped = flipped; setNeedsCommit(); }
140    bool geometryFlipped() const { return m_geometryFlipped; }
141
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
148    void setOwner(GraphicsLayerChromium* owner) { m_owner = owner; }
149
150    void setReplicaLayer(LayerChromium* layer) { m_replicaLayer = layer; }
151    LayerChromium* replicaLayer() { return m_replicaLayer; }
152
153    // These methods typically need to be overwritten by derived classes.
154    virtual bool drawsContent() const { return false; }
155    virtual void paintContentsIfDirty(const IntRect&) { }
156    virtual void paintContentsIfDirty() { }
157    virtual void updateCompositorResources() { }
158    virtual void setIsMask(bool) {}
159    virtual void unreserveContentsTexture() { }
160    virtual void bindContentsTexture() { }
161    virtual void draw(const IntRect&) { }
162
163    // These exists just for debugging (via drawDebugBorder()).
164    void setBorderColor(const Color&);
165
166#ifndef NDEBUG
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;
190protected:
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
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;
224private:
225    void setNeedsCommit();
226
227    void setSuperlayer(LayerChromium* superlayer) { m_superlayer = superlayer; }
228
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