61c1d4a877bebccfa5da1fd1deb64231922a2c71
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / LayerRendererChromium.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 LayerRendererChromium_h
33 #define LayerRendererChromium_h
34
35 #if USE(ACCELERATED_COMPOSITING)
36
37 #include "ContentLayerChromium.h"
38 #include "FloatQuad.h"
39 #include "IntRect.h"
40 #include "LayerChromium.h"
41 #include "TrackingTextureAllocator.h"
42 #include "VideoLayerChromium.h"
43 #include "cc/CCDrawQuad.h"
44 #include "cc/CCHeadsUpDisplay.h"
45 #include "cc/CCLayerTreeHost.h"
46 #include "cc/CCTextureLayerImpl.h"
47 #include "cc/CCVideoLayerImpl.h"
48 #include <wtf/HashMap.h>
49 #include <wtf/Noncopyable.h>
50 #include <wtf/PassOwnPtr.h>
51 #include <wtf/PassRefPtr.h>
52 #include <wtf/Vector.h>
53
54 namespace WebCore {
55
56 class CCHeadsUpDisplay;
57 class CCLayerImpl;
58 class CCRenderPass;
59 class CCTextureDrawQuad;
60 class GeometryBinding;
61 class GraphicsContext3D;
62 class TrackingTextureAllocator;
63 class LayerRendererSwapBuffersCompleteCallbackAdapter;
64 class ScopedEnsureFramebufferAllocation;
65
66 class LayerRendererChromiumClient {
67 public:
68     virtual const IntSize& viewportSize() const = 0;
69     virtual const CCSettings& settings() const = 0;
70     virtual CCLayerImpl* rootLayer() = 0;
71     virtual const CCLayerImpl* rootLayer() const = 0;
72     virtual void didLoseContext() = 0;
73     virtual void onSwapBuffersComplete() = 0;
74     virtual void setFullRootLayerDamage() = 0;
75 };
76
77 // Class that handles drawing of composited render layers using GL.
78 class LayerRendererChromium {
79     WTF_MAKE_NONCOPYABLE(LayerRendererChromium);
80 public:
81     static PassOwnPtr<LayerRendererChromium> create(LayerRendererChromiumClient*, PassRefPtr<GraphicsContext3D>);
82
83     // Must be called in order to allow the LayerRendererChromium to destruct
84     void close();
85
86     ~LayerRendererChromium();
87
88     const CCSettings& settings() const { return m_client->settings(); }
89     const LayerRendererCapabilities& capabilities() const { return m_capabilities; }
90
91     CCLayerImpl* rootLayer() { return m_client->rootLayer(); }
92     const CCLayerImpl* rootLayer() const { return m_client->rootLayer(); }
93
94     GraphicsContext3D* context();
95     bool contextSupportsMapSub() const { return m_capabilities.usingMapSub; }
96
97     const IntSize& viewportSize() { return m_client->viewportSize(); }
98     int viewportWidth() { return viewportSize().width(); }
99     int viewportHeight() { return viewportSize().height(); }
100
101     void viewportChanged();
102
103     void beginDrawingFrame();
104     void drawRenderPass(const CCRenderPass*);
105     void finishDrawingFrame();
106
107     // waits for rendering to finish
108     void finish();
109
110     // puts backbuffer onscreen
111     void swapBuffers(const IntRect& subBuffer);
112
113     static void debugGLCall(GraphicsContext3D*, const char* command, const char* file, int line);
114
115     const TransformationMatrix& projectionMatrix() const { return m_projectionMatrix; }
116     const TransformationMatrix& windowMatrix() const { return m_windowMatrix; }
117
118     const GeometryBinding* sharedGeometry() const { return m_sharedGeometry.get(); }
119     const FloatQuad& sharedGeometryQuad() const { return m_sharedGeometryQuad; }
120     const LayerChromium::BorderProgram* borderProgram();
121     const CCHeadsUpDisplay::Program* headsUpDisplayProgram();
122     const CCRenderSurface::Program* renderSurfaceProgram();
123     const CCRenderSurface::ProgramAA* renderSurfaceProgramAA();
124     const CCRenderSurface::MaskProgram* renderSurfaceMaskProgram();
125     const CCRenderSurface::MaskProgramAA* renderSurfaceMaskProgramAA();
126     const CCTextureLayerImpl::ProgramFlip* textureLayerProgramFlip();
127     const CCTextureLayerImpl::ProgramStretch* textureLayerProgramStretch();
128     const CCTextureLayerImpl::ProgramStretchFlip* textureLayerProgramStretchFlip();
129     const CCTextureLayerImpl::TexRectProgram* textureLayerTexRectProgram();
130     const CCTextureLayerImpl::TexRectProgramFlip* textureLayerTexRectProgramFlip();
131     const CCTiledLayerImpl::Program* tilerProgram();
132     const CCTiledLayerImpl::ProgramOpaque* tilerProgramOpaque();
133     const CCTiledLayerImpl::ProgramAA* tilerProgramAA();
134     const CCTiledLayerImpl::ProgramSwizzle* tilerProgramSwizzle();
135     const CCTiledLayerImpl::ProgramSwizzleOpaque* tilerProgramSwizzleOpaque();
136     const CCTiledLayerImpl::ProgramSwizzleAA* tilerProgramSwizzleAA();
137     const CCVideoLayerImpl::RGBAProgram* videoLayerRGBAProgram();
138     const CCVideoLayerImpl::YUVProgram* videoLayerYUVProgram();
139     const CCVideoLayerImpl::NativeTextureProgram* videoLayerNativeTextureProgram();
140     const CCVideoLayerImpl::StreamTextureProgram* streamTextureLayerProgram();
141
142     void getFramebufferPixels(void *pixels, const IntRect&);
143
144     TextureManager* renderSurfaceTextureManager() const { return m_renderSurfaceTextureManager.get(); }
145     TextureAllocator* renderSurfaceTextureAllocator() const { return m_renderSurfaceTextureAllocator.get(); }
146     TextureAllocator* contentsTextureAllocator() const { return m_contentsTextureAllocator.get(); }
147
148     CCHeadsUpDisplay* headsUpDisplay() { return m_headsUpDisplay.get(); }
149
150     void setScissorToRect(const IntRect&);
151
152     String layerTreeAsText() const;
153
154     bool isContextLost();
155
156     void setVisible(bool);
157
158     GC3Denum bestTextureFormat();
159
160     static void toGLMatrix(float*, const TransformationMatrix&);
161     void drawTexturedQuad(const TransformationMatrix& layerMatrix,
162                           float width, float height, float opacity, const FloatQuad&,
163                           int matrixLocation, int alphaLocation, int quadLocation);
164
165     void discardFramebuffer();
166     void ensureFramebuffer();
167     bool isFramebufferDiscarded() const { return m_isFramebufferDiscarded; }
168
169 protected:
170     LayerRendererChromium(LayerRendererChromiumClient*, PassRefPtr<GraphicsContext3D>);
171     bool initialize();
172
173 private:
174     void drawQuad(const CCDrawQuad*, const FloatRect& surfaceDamageRect);
175     void drawDebugBorderQuad(const CCDebugBorderDrawQuad*);
176     void drawRenderSurfaceQuad(const CCRenderSurfaceDrawQuad*);
177     void drawSolidColorQuad(const CCSolidColorDrawQuad*);
178     void drawTextureQuad(const CCTextureDrawQuad*);
179     void drawTileQuad(const CCTileDrawQuad*);
180     void drawVideoQuad(const CCVideoDrawQuad*);
181
182     ManagedTexture* getOffscreenLayerTexture();
183     void copyPlaneToTexture(const CCVideoDrawQuad*, const void* plane, int index);
184     bool copyFrameToTextures(const CCVideoDrawQuad*);
185     template<class Program> void drawSingleTextureVideoQuad(const CCVideoDrawQuad*, Program*, float widthScaleFactor, Platform3DObject textureId, GC3Denum target);
186     void drawNativeTexture2D(const CCVideoDrawQuad*);
187     void drawStreamTexture(const CCVideoDrawQuad*);
188     void drawRGBA(const CCVideoDrawQuad*);
189     void drawYUV(const CCVideoDrawQuad*);
190
191     void copyOffscreenTextureToDisplay();
192
193     void setDrawViewportRect(const IntRect&, bool flipY);
194
195     bool useRenderSurface(CCRenderSurface*);
196     void clearRenderSurface(CCRenderSurface*, CCRenderSurface* rootRenderSurface, const FloatRect& surfaceDamageRect);
197
198     void releaseRenderSurfaceTextures();
199
200     bool makeContextCurrent();
201
202     static bool compareLayerZ(const RefPtr<CCLayerImpl>&, const RefPtr<CCLayerImpl>&);
203
204     void dumpRenderSurfaces(TextStream&, int indent, const CCLayerImpl*) const;
205
206     bool initializeSharedObjects();
207     void cleanupSharedObjects();
208
209     void clearRenderSurfacesOnCCLayerImplRecursive(CCLayerImpl*);
210
211     friend class LayerRendererSwapBuffersCompleteCallbackAdapter;
212     void onSwapBuffersComplete();
213
214     LayerRendererChromiumClient* m_client;
215
216     LayerRendererCapabilities m_capabilities;
217
218     TransformationMatrix m_projectionMatrix;
219     TransformationMatrix m_windowMatrix;
220
221     CCRenderSurface* m_currentRenderSurface;
222     unsigned m_offscreenFramebufferId;
223
224     // Store values that are shared between instances of each layer type
225     // associated with this instance of the compositor. Since there can be
226     // multiple instances of the compositor running in the same renderer process
227     // we cannot store these values in static variables.
228     OwnPtr<GeometryBinding> m_sharedGeometry;
229     OwnPtr<LayerChromium::BorderProgram> m_borderProgram;
230     OwnPtr<CCHeadsUpDisplay::Program> m_headsUpDisplayProgram;
231     OwnPtr<CCTextureLayerImpl::ProgramFlip> m_textureLayerProgramFlip;
232     OwnPtr<CCTextureLayerImpl::ProgramStretch> m_textureLayerProgramStretch;
233     OwnPtr<CCTextureLayerImpl::ProgramStretchFlip> m_textureLayerProgramStretchFlip;
234     OwnPtr<CCTextureLayerImpl::TexRectProgram> m_textureLayerTexRectProgram;
235     OwnPtr<CCTextureLayerImpl::TexRectProgramFlip> m_textureLayerTexRectProgramFlip;
236     OwnPtr<CCTiledLayerImpl::Program> m_tilerProgram;
237     OwnPtr<CCTiledLayerImpl::ProgramOpaque> m_tilerProgramOpaque;
238     OwnPtr<CCTiledLayerImpl::ProgramSwizzle> m_tilerProgramSwizzle;
239     OwnPtr<CCTiledLayerImpl::ProgramSwizzleOpaque> m_tilerProgramSwizzleOpaque;
240     OwnPtr<CCTiledLayerImpl::ProgramAA> m_tilerProgramAA;
241     OwnPtr<CCTiledLayerImpl::ProgramSwizzleAA> m_tilerProgramSwizzleAA;
242     OwnPtr<CCRenderSurface::MaskProgram> m_renderSurfaceMaskProgram;
243     OwnPtr<CCRenderSurface::Program> m_renderSurfaceProgram;
244     OwnPtr<CCRenderSurface::MaskProgramAA> m_renderSurfaceMaskProgramAA;
245     OwnPtr<CCRenderSurface::ProgramAA> m_renderSurfaceProgramAA;
246     OwnPtr<CCVideoLayerImpl::RGBAProgram> m_videoLayerRGBAProgram;
247     OwnPtr<CCVideoLayerImpl::YUVProgram> m_videoLayerYUVProgram;
248     OwnPtr<CCVideoLayerImpl::NativeTextureProgram> m_videoLayerNativeTextureProgram;
249     OwnPtr<CCVideoLayerImpl::StreamTextureProgram> m_streamTextureLayerProgram;
250
251     OwnPtr<TextureManager> m_renderSurfaceTextureManager;
252     OwnPtr<TrackingTextureAllocator> m_contentsTextureAllocator;
253     OwnPtr<TrackingTextureAllocator> m_renderSurfaceTextureAllocator;
254
255     OwnPtr<CCHeadsUpDisplay> m_headsUpDisplay;
256
257     RefPtr<GraphicsContext3D> m_context;
258
259     CCRenderSurface* m_defaultRenderSurface;
260
261     FloatQuad m_sharedGeometryQuad;
262
263     bool m_isViewportChanged;
264     bool m_isFramebufferDiscarded;
265 };
266
267 // The purpose of this helper is twofold:
268 // 1. To ensure that a framebuffer is available for scope lifetime, and
269 // 2. To reset framebuffer allocation to previous state on scope exit.
270 // If the framebuffer is recreated, its contents are undefined.
271 // FIXME: Prevent/delay discarding framebuffer via any means while any
272 // instance of this is alive. At the moment, this isn't an issue.
273 class ScopedEnsureFramebufferAllocation {
274 public:
275     explicit ScopedEnsureFramebufferAllocation(LayerRendererChromium* layerRenderer)
276         : m_layerRenderer(layerRenderer)
277         , m_framebufferWasInitiallyDiscarded(layerRenderer->isFramebufferDiscarded())
278     {
279         if (m_framebufferWasInitiallyDiscarded)
280             m_layerRenderer->ensureFramebuffer();
281     }
282
283     ~ScopedEnsureFramebufferAllocation()
284     {
285         if (m_framebufferWasInitiallyDiscarded)
286             m_layerRenderer->discardFramebuffer();
287     }
288
289 private:
290     LayerRendererChromium* m_layerRenderer;
291     bool m_framebufferWasInitiallyDiscarded;
292 };
293
294
295 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
296 // call made by the compositor. Useful for debugging rendering issues but
297 // will significantly degrade performance.
298 #define DEBUG_GL_CALLS 0
299
300 #if DEBUG_GL_CALLS && !defined ( NDEBUG )
301 #define GLC(context, x) { (x), LayerRendererChromium::debugGLCall(context, #x, __FILE__, __LINE__); }
302 #else
303 #define GLC(context, x) (x)
304 #endif
305
306
307 }
308
309 #endif // USE(ACCELERATED_COMPOSITING)
310
311 #endif