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 LayerRendererChromium_h
33 #define LayerRendererChromium_h
35 #if USE(ACCELERATED_COMPOSITING)
37 #include "CanvasLayerChromium.h"
38 #include "ContentLayerChromium.h"
40 #include "LayerChromium.h"
41 #include "PluginLayerChromium.h"
43 #include "VideoLayerChromium.h"
44 #include <wtf/HashMap.h>
45 #include <wtf/Noncopyable.h>
46 #include <wtf/PassOwnPtr.h>
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/RefCounted.h>
49 #include <wtf/Vector.h>
52 #include <CoreGraphics/CGContext.h>
53 #include <wtf/RetainPtr.h>
58 class GraphicsContext3D;
60 // Class that handles drawing of composited render layers using GL.
61 class LayerRendererChromium : public RefCounted<LayerRendererChromium> {
63 static PassRefPtr<LayerRendererChromium> create(PassRefPtr<GraphicsContext3D> graphicsContext3D);
65 ~LayerRendererChromium();
67 GraphicsContext3D* context();
69 // updates size of root texture, if needed, and scrolls the backbuffer.
70 void prepareToDrawLayers(const IntRect& visibleRect, const IntRect& contentRect, const IntPoint& scrollPosition);
72 // updates a rectangle within the root layer texture
73 void updateRootLayerTextureRect(const IntRect& updateRect);
75 // draws the current layers onto the backbuffer
76 void drawLayers(const IntRect& visibleRect, const IntRect& contentRect);
78 // waits for rendering to finish
81 // puts backbuffer onscreen
84 void setRootLayer(PassRefPtr<LayerChromium> layer) { m_rootLayer = layer; }
85 LayerChromium* rootLayer() { return m_rootLayer.get(); }
86 void transferRootLayer(LayerRendererChromium* other) { other->m_rootLayer = m_rootLayer.release(); }
88 bool hardwareCompositing() const { return m_hardwareCompositing; }
90 void setRootLayerCanvasSize(const IntSize&);
92 GraphicsContext* rootLayerGraphicsContext() const { return m_rootLayerGraphicsContext.get(); }
94 unsigned createLayerTexture();
95 void deleteLayerTexture(unsigned);
97 IntRect currentScissorRect() const { return m_currentScissorRect; }
99 static void debugGLCall(GraphicsContext3D*, const char* command, const char* file, int line);
101 const TransformationMatrix& projectionMatrix() const { return m_projectionMatrix; }
103 void useShader(unsigned);
105 bool checkTextureSize(const IntSize&);
107 const LayerChromium::SharedValues* layerSharedValues() const { return m_layerSharedValues.get(); }
108 const ContentLayerChromium::SharedValues* contentLayerSharedValues() const { return m_contentLayerSharedValues.get(); }
109 const CanvasLayerChromium::SharedValues* canvasLayerSharedValues() const { return m_canvasLayerSharedValues.get(); }
110 const VideoLayerChromium::SharedValues* videoLayerSharedValues() const { return m_videoLayerSharedValues.get(); }
111 const PluginLayerChromium::SharedValues* pluginLayerSharedValues() const { return m_pluginLayerSharedValues.get(); }
113 void resizeOnscreenContent(const IntSize&);
115 IntSize rootLayerTextureSize() const { return IntSize(m_rootLayerTextureWidth, m_rootLayerTextureHeight); }
116 IntRect rootLayerContentRect() const { return m_rootContentRect; }
117 void getFramebufferPixels(void *pixels, const IntRect& rect);
120 explicit LayerRendererChromium(PassRefPtr<GraphicsContext3D> graphicsContext3D);
122 void updateLayersRecursive(LayerChromium* layer, const TransformationMatrix& parentMatrix, float opacity);
124 void drawLayersRecursive(LayerChromium*);
126 void drawLayer(LayerChromium*);
128 bool isLayerVisible(LayerChromium*, const TransformationMatrix&, const IntRect& visibleRect);
130 void drawLayerIntoStencilBuffer(LayerChromium*, bool decrement);
132 void scissorToRect(const IntRect&);
134 bool makeContextCurrent();
136 bool initializeSharedObjects();
137 void cleanupSharedObjects();
139 unsigned m_rootLayerTextureId;
140 int m_rootLayerTextureWidth;
141 int m_rootLayerTextureHeight;
143 // Scroll shader uniform locations.
144 unsigned m_scrollShaderProgram;
145 int m_scrollShaderSamplerLocation;
146 int m_scrollShaderMatrixLocation;
148 TransformationMatrix m_projectionMatrix;
150 RefPtr<LayerChromium> m_rootLayer;
152 IntPoint m_scrollPosition;
153 bool m_hardwareCompositing;
155 unsigned int m_currentShader;
158 OwnPtr<skia::PlatformCanvas> m_rootLayerCanvas;
159 OwnPtr<PlatformContextSkia> m_rootLayerSkiaContext;
160 OwnPtr<GraphicsContext> m_rootLayerGraphicsContext;
162 Vector<uint8_t> m_rootLayerBackingStore;
163 RetainPtr<CGContextRef> m_rootLayerCGContext;
164 OwnPtr<GraphicsContext> m_rootLayerGraphicsContext;
167 IntSize m_rootLayerCanvasSize;
169 IntRect m_rootVisibleRect;
170 IntRect m_rootContentRect;
171 IntRect m_currentScissorRect;
173 int m_maxTextureSize;
175 int m_numStencilBits;
177 // Store values that are shared between instances of each layer type
178 // associated with this instance of the compositor. Since there can be
179 // multiple instances of the compositor running in the same renderer process
180 // we cannot store these values in static variables.
181 OwnPtr<LayerChromium::SharedValues> m_layerSharedValues;
182 OwnPtr<ContentLayerChromium::SharedValues> m_contentLayerSharedValues;
183 OwnPtr<CanvasLayerChromium::SharedValues> m_canvasLayerSharedValues;
184 OwnPtr<VideoLayerChromium::SharedValues> m_videoLayerSharedValues;
185 OwnPtr<PluginLayerChromium::SharedValues> m_pluginLayerSharedValues;
187 RefPtr<GraphicsContext3D> m_context;
190 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
191 // call made by the compositor. Useful for debugging rendering issues but
192 // will significantly degrade performance.
193 #define DEBUG_GL_CALLS 0
195 #if DEBUG_GL_CALLS && !defined ( NDEBUG )
196 #define GLC(context, x) { (x), LayerRendererChromium::debugGLCall(context, #x, __FILE__, __LINE__); }
198 #define GLC(context, x) (x)
204 #endif // USE(ACCELERATED_COMPOSITING)