2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #ifndef LayerChromium_h
33 #define LayerChromium_h
35 #if USE(ACCELERATED_COMPOSITING)
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"
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>
55 class CCLayerTreeHost;
56 class GraphicsContext3D;
58 class CCLayerDelegate {
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;
67 // Base class for composited layers. Special layer types are derived from
69 class LayerChromium : public RefCounted<LayerChromium> {
70 friend class LayerTilerChromium;
72 static PassRefPtr<LayerChromium> create(CCLayerDelegate* = 0);
74 virtual ~LayerChromium();
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; }
86 void setAnchorPoint(const FloatPoint& anchorPoint) { m_anchorPoint = anchorPoint; setNeedsCommit(); }
87 FloatPoint anchorPoint() const { return m_anchorPoint; }
89 void setAnchorPointZ(float anchorPointZ) { m_anchorPointZ = anchorPointZ; setNeedsCommit(); }
90 float anchorPointZ() const { return m_anchorPointZ; }
92 void setBackgroundColor(const Color& color) { m_backgroundColor = color; setNeedsCommit(); }
93 Color backgroundColor() const { return m_backgroundColor; }
95 void setBounds(const IntSize&);
96 const IntSize& bounds() const { return m_bounds; }
97 virtual IntSize contentBounds() const { return bounds(); }
99 void setClearsContext(bool clears) { m_clearsContext = clears; setNeedsCommit(); }
100 bool clearsContext() const { return m_clearsContext; }
102 void setFrame(const FloatRect&);
103 FloatRect frame() const { return m_frame; }
105 void setHidden(bool hidden) { m_hidden = hidden; setNeedsCommit(); }
106 bool isHidden() const { return m_hidden; }
108 void setMasksToBounds(bool masksToBounds) { m_masksToBounds = masksToBounds; }
109 bool masksToBounds() const { return m_masksToBounds; }
111 void setName(const String&);
112 const String& name() const { return m_name; }
114 void setMaskLayer(LayerChromium* maskLayer) { m_maskLayer = maskLayer; }
115 LayerChromium* maskLayer() const { return m_maskLayer.get(); }
117 void setNeedsDisplay(const FloatRect& dirtyRect);
118 void setNeedsDisplay();
119 const FloatRect& dirtyRect() const { return m_dirtyRect; }
120 void resetNeedsDisplay();
122 void setNeedsDisplayOnBoundsChange(bool needsDisplay) { m_needsDisplayOnBoundsChange = needsDisplay; }
124 void setOpacity(float opacity) { m_opacity = opacity; setNeedsCommit(); }
125 float opacity() const { return m_opacity; }
127 void setOpaque(bool opaque) { m_opaque = opaque; setNeedsCommit(); }
128 bool opaque() const { return m_opaque; }
130 void setPosition(const FloatPoint& position) { m_position = position; setNeedsCommit(); }
131 FloatPoint position() const { return m_position; }
133 void setZPosition(float zPosition) { m_zPosition = zPosition; setNeedsCommit(); }
134 float zPosition() const { return m_zPosition; }
136 void setSublayerTransform(const TransformationMatrix& transform) { m_sublayerTransform = transform; setNeedsCommit(); }
137 const TransformationMatrix& sublayerTransform() const { return m_sublayerTransform; }
139 void setTransform(const TransformationMatrix& transform) { m_transform = transform; setNeedsCommit(); }
140 const TransformationMatrix& transform() const { return m_transform; }
142 const IntRect& visibleLayerRect() const { return m_visibleLayerRect; }
143 void setVisibleLayerRect(const IntRect& visibleLayerRect) { m_visibleLayerRect = visibleLayerRect; }
145 const IntPoint& scrollPosition() const { return m_scrollPosition; }
146 void setScrollPosition(const IntPoint& scrollPosition) { m_scrollPosition = scrollPosition; }
148 bool doubleSided() const { return m_doubleSided; }
149 void setDoubleSided(bool doubleSided) { m_doubleSided = doubleSided; setNeedsCommit(); }
151 // FIXME: This setting is currently ignored.
152 void setGeometryFlipped(bool flipped) { m_geometryFlipped = flipped; setNeedsCommit(); }
153 bool geometryFlipped() const { return m_geometryFlipped; }
155 bool preserves3D() { return m_delegate && m_delegate->preserves3D(); }
157 void setUsesLayerScissor(bool usesLayerScissor) { m_usesLayerScissor = usesLayerScissor; }
158 bool usesLayerScissor() const { return m_usesLayerScissor; }
160 void setIsRootLayer(bool isRootLayer) { m_isRootLayer = isRootLayer; }
161 bool isRootLayer() const { return m_isRootLayer; }
163 virtual void setLayerTreeHost(CCLayerTreeHost*);
165 void setDelegate(CCLayerDelegate* delegate) { m_delegate = delegate; }
167 void setReplicaLayer(LayerChromium* layer) { m_replicaLayer = layer; }
168 LayerChromium* replicaLayer() { return m_replicaLayer.get(); }
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() { }
179 // These exists just for debugging (via drawDebugBorder()).
180 void setBorderColor(const Color&);
182 void drawDebugBorder();
184 void setBorderWidth(float);
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);
190 virtual void pushPropertiesTo(CCLayerImpl*);
192 typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> BorderProgram;
194 int id() const { return m_layerId; }
196 void clearRenderSurface() { m_renderSurface.clear(); }
197 RenderSurfaceChromium* renderSurface() const { return m_renderSurface.get(); }
198 void createRenderSurface();
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; }
213 // Returns true if any of the layer's descendants has content to draw.
214 bool descendantDrawsContent();
216 CCLayerTreeHost* layerTreeHost() const { return m_layerTreeHost.get(); }
217 void cleanupResourcesRecursive();
220 CCLayerDelegate* m_delegate;
221 explicit LayerChromium(CCLayerDelegate*);
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();
228 static void toGLMatrix(float*, const TransformationMatrix&);
230 FloatRect m_dirtyRect;
231 bool m_contentsDirty;
233 RefPtr<LayerChromium> m_maskLayer;
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();
242 void setNeedsCommit();
244 void setParent(LayerChromium*);
245 bool hasAncestor(LayerChromium*) const;
247 size_t numChildren() const
249 return m_children.size();
252 // Returns the index of the child or -1 if not found.
253 int indexOfChild(const LayerChromium*);
255 // This should only be called from removeFromParent.
256 void removeChild(LayerChromium*);
258 Vector<RefPtr<LayerChromium> > m_children;
259 LayerChromium* m_parent;
261 RefPtr<CCLayerTreeHost> m_layerTreeHost;
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;
274 float m_anchorPointZ;
275 bool m_clearsContext;
277 bool m_masksToBounds;
279 bool m_geometryFlipped;
280 bool m_needsDisplayOnBoundsChange;
282 bool m_usesLayerScissor;
285 TransformationMatrix m_transform;
286 TransformationMatrix m_sublayerTransform;
290 // Replica layer used for reflections.
291 RefPtr<LayerChromium> m_replicaLayer;
293 // Transient properties.
294 OwnPtr<RenderSurfaceChromium> m_renderSurface;
296 IntRect m_scissorRect;
297 RenderSurfaceChromium* m_targetRenderSurface;
298 TransformationMatrix m_drawTransform;
299 TransformationMatrix m_screenSpaceTransform;
300 IntRect m_drawableContentRect;
305 void sortLayers(Vector<RefPtr<LayerChromium> >::iterator, Vector<RefPtr<LayerChromium> >::iterator, void*);
308 #endif // USE(ACCELERATED_COMPOSITING)