[chromium] Separate IOSurface layer type from texture layers
[WebKit.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 "FloatQuad.h"
38 #include "IntRect.h"
39 #include "TextureCopier.h"
40 #include "TextureUploader.h"
41 #include "TrackingTextureAllocator.h"
42 #include "cc/CCLayerTreeHost.h"
43 #include <wtf/HashMap.h>
44 #include <wtf/Noncopyable.h>
45 #include <wtf/PassOwnPtr.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/Vector.h>
48
49 namespace WebCore {
50
51 class CCCheckerboardDrawQuad;
52 class CCDebugBorderDrawQuad;
53 class CCDrawQuad;
54 class CCIOSurfaceDrawQuad;
55 class CCRenderPass;
56 class CCRenderSurfaceDrawQuad;
57 class CCSolidColorDrawQuad;
58 class CCTextureDrawQuad;
59 class CCTileDrawQuad;
60 class CCVideoDrawQuad;
61 class GeometryBinding;
62 class GraphicsContext3D;
63 class LayerRendererGpuMemoryAllocationChangedCallbackAdapter;
64 class LayerRendererSwapBuffersCompleteCallbackAdapter;
65 class ScopedEnsureFramebufferAllocation;
66
67 class LayerRendererChromiumClient {
68 public:
69     virtual const IntSize& viewportSize() const = 0;
70     virtual const CCSettings& settings() const = 0;
71     virtual void didLoseContext() = 0;
72     virtual void onSwapBuffersComplete() = 0;
73     virtual void setFullRootLayerDamage() = 0;
74 };
75
76 // Class that handles drawing of composited render layers using GL.
77 class LayerRendererChromium {
78     WTF_MAKE_NONCOPYABLE(LayerRendererChromium);
79 public:
80     static PassOwnPtr<LayerRendererChromium> create(LayerRendererChromiumClient*, PassRefPtr<GraphicsContext3D>);
81
82     ~LayerRendererChromium();
83
84     const CCSettings& settings() const { return m_client->settings(); }
85     const LayerRendererCapabilities& capabilities() const { return m_capabilities; }
86
87     GraphicsContext3D* context();
88     bool contextSupportsMapSub() const { return m_capabilities.usingMapSub; }
89
90     const IntSize& viewportSize() { return m_client->viewportSize(); }
91     int viewportWidth() { return viewportSize().width(); }
92     int viewportHeight() { return viewportSize().height(); }
93
94     void viewportChanged();
95
96     void beginDrawingFrame(CCRenderSurface* defaultRenderSurface);
97     void drawRenderPass(const CCRenderPass*);
98     void finishDrawingFrame();
99
100     void drawHeadsUpDisplay(ManagedTexture*, const IntSize& hudSize);
101
102     // waits for rendering to finish
103     void finish();
104
105     void doNoOp();
106     // puts backbuffer onscreen
107     bool swapBuffers(const IntRect& subBuffer);
108
109     static void debugGLCall(GraphicsContext3D*, const char* command, const char* file, int line);
110
111     const TransformationMatrix& projectionMatrix() const { return m_projectionMatrix; }
112     const TransformationMatrix& windowMatrix() const { return m_windowMatrix; }
113
114     const GeometryBinding* sharedGeometry() const { return m_sharedGeometry.get(); }
115     const FloatQuad& sharedGeometryQuad() const { return m_sharedGeometryQuad; }
116
117
118     void getFramebufferPixels(void *pixels, const IntRect&);
119     bool getFramebufferTexture(ManagedTexture*, const IntRect& deviceRect);
120
121     TextureManager* renderSurfaceTextureManager() const { return m_renderSurfaceTextureManager.get(); }
122     TextureCopier* textureCopier() const { return m_textureCopier.get(); }
123     TextureUploader* textureUploader() const { return m_textureUploader.get(); }
124     TextureAllocator* renderSurfaceTextureAllocator() const { return m_renderSurfaceTextureAllocator.get(); }
125     TextureAllocator* contentsTextureAllocator() const { return m_contentsTextureAllocator.get(); }
126
127     void setScissorToRect(const IntRect&);
128
129     bool isContextLost();
130
131     void setVisible(bool);
132
133     GC3Denum bestTextureFormat();
134
135     static void toGLMatrix(float*, const TransformationMatrix&);
136     void drawTexturedQuad(const TransformationMatrix& layerMatrix,
137                           float width, float height, float opacity, const FloatQuad&,
138                           int matrixLocation, int alphaLocation, int quadLocation);
139
140 protected:
141     friend class LayerRendererGpuMemoryAllocationChangedCallbackAdapter;
142     void discardFramebuffer();
143     void ensureFramebuffer();
144     bool isFramebufferDiscarded() const { return m_isFramebufferDiscarded; }
145
146     LayerRendererChromium(LayerRendererChromiumClient*, PassRefPtr<GraphicsContext3D>);
147     bool initialize();
148
149 private:
150     void drawQuad(const CCDrawQuad*, const FloatRect& surfaceDamageRect);
151     void drawCheckerboardQuad(const CCCheckerboardDrawQuad*);
152     void drawDebugBorderQuad(const CCDebugBorderDrawQuad*);
153     void drawBackgroundFilters(const CCRenderSurfaceDrawQuad*);
154     void drawRenderSurfaceQuad(const CCRenderSurfaceDrawQuad*);
155     void drawSolidColorQuad(const CCSolidColorDrawQuad*);
156     void drawTextureQuad(const CCTextureDrawQuad*);
157     void drawIOSurfaceQuad(const CCIOSurfaceDrawQuad*);
158     void drawTileQuad(const CCTileDrawQuad*);
159     void drawVideoQuad(const CCVideoDrawQuad*);
160
161     void copyPlaneToTexture(const CCVideoDrawQuad*, const void* plane, int index);
162     bool copyFrameToTextures(const CCVideoDrawQuad*);
163     template<class Program> void drawSingleTextureVideoQuad(const CCVideoDrawQuad*, Program*, float widthScaleFactor, Platform3DObject textureId, GC3Denum target);
164     void drawNativeTexture2D(const CCVideoDrawQuad*);
165     void drawStreamTexture(const CCVideoDrawQuad*);
166     void drawRGBA(const CCVideoDrawQuad*);
167     void drawYUV(const CCVideoDrawQuad*);
168
169     void setDrawViewportRect(const IntRect&, bool flipY);
170
171     // The current drawing target is either a RenderSurface or ManagedTexture. Use these functions to switch to a new drawing target.
172     bool useRenderSurface(CCRenderSurface*);
173     bool useManagedTexture(ManagedTexture*, const IntRect& viewportRect);
174     bool isCurrentRenderSurface(CCRenderSurface*);
175
176     bool bindFramebufferToTexture(ManagedTexture*, const IntRect& viewportRect);
177
178     void clearRenderSurface(CCRenderSurface*, CCRenderSurface* rootRenderSurface, const FloatRect& surfaceDamageRect);
179
180     void releaseRenderSurfaceTextures();
181
182     bool makeContextCurrent();
183
184     bool initializeSharedObjects();
185     void cleanupSharedObjects();
186
187     friend class LayerRendererSwapBuffersCompleteCallbackAdapter;
188     void onSwapBuffersComplete();
189
190     LayerRendererChromiumClient* m_client;
191
192     LayerRendererCapabilities m_capabilities;
193
194     TransformationMatrix m_projectionMatrix;
195     TransformationMatrix m_windowMatrix;
196
197     CCRenderSurface* m_currentRenderSurface;
198     ManagedTexture* m_currentManagedTexture;
199     unsigned m_offscreenFramebufferId;
200
201     OwnPtr<GeometryBinding> m_sharedGeometry;
202
203     // This block of bindings defines all of the programs used by the compositor itself.
204
205     // Tiled layer shaders.
206     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha> TileProgram;
207     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexClampAlphaAA> TileProgramAA;
208     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexClampSwizzleAlphaAA> TileProgramSwizzleAA;
209     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque> TileProgramOpaque;
210     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha> TileProgramSwizzle;
211     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque> TileProgramSwizzleOpaque;
212     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard> TileCheckerboardProgram;
213
214     // Render surface shaders.
215     // CCRenderSurface::drawLayers() needs to see these programs currently.
216     // FIXME: Draw with a quad type for render surfaces and get rid of this friendlyness.
217     friend class CCRenderSurface;
218     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexAlpha> RenderSurfaceProgram;
219     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexAlphaMask> RenderSurfaceMaskProgram;
220     typedef ProgramBinding<VertexShaderQuad, FragmentShaderRGBATexAlphaAA> RenderSurfaceProgramAA;
221     typedef ProgramBinding<VertexShaderQuad, FragmentShaderRGBATexAlphaMaskAA> RenderSurfaceMaskProgramAA;
222
223     // Texture shaders.
224     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexAlpha> TextureProgram;
225     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexFlipAlpha> TextureProgramFlip;
226     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexRectFlipAlpha> TextureIOSurfaceProgram;
227
228     // Video shaders.
229     typedef ProgramBinding<VertexShaderVideoTransform, FragmentShaderOESImageExternal> VideoStreamTextureProgram;
230     typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo> VideoYUVProgram;
231
232     // Special purpose / effects shaders.
233     typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> SolidColorProgram;
234
235     // Debugging shaders.
236     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexSwizzleAlpha> HeadsUpDisplayProgram;
237
238
239     const TileProgram* tileProgram();
240     const TileProgramOpaque* tileProgramOpaque();
241     const TileProgramAA* tileProgramAA();
242     const TileProgramSwizzle* tileProgramSwizzle();
243     const TileProgramSwizzleOpaque* tileProgramSwizzleOpaque();
244     const TileProgramSwizzleAA* tileProgramSwizzleAA();
245     const TileCheckerboardProgram* tileCheckerboardProgram();
246
247     const RenderSurfaceProgram* renderSurfaceProgram();
248     const RenderSurfaceProgramAA* renderSurfaceProgramAA();
249     const RenderSurfaceMaskProgram* renderSurfaceMaskProgram();
250     const RenderSurfaceMaskProgramAA* renderSurfaceMaskProgramAA();
251
252     const TextureProgram* textureProgram();
253     const TextureProgramFlip* textureProgramFlip();
254     const TextureIOSurfaceProgram* textureIOSurfaceProgram();
255
256     const VideoYUVProgram* videoYUVProgram();
257     const VideoStreamTextureProgram* videoStreamTextureProgram();
258
259     const SolidColorProgram* solidColorProgram();
260
261     const HeadsUpDisplayProgram* headsUpDisplayProgram();
262
263     OwnPtr<TileProgram> m_tileProgram;
264     OwnPtr<TileProgramOpaque> m_tileProgramOpaque;
265     OwnPtr<TileProgramAA> m_tileProgramAA;
266     OwnPtr<TileProgramSwizzle> m_tileProgramSwizzle;
267     OwnPtr<TileProgramSwizzleOpaque> m_tileProgramSwizzleOpaque;
268     OwnPtr<TileProgramSwizzleAA> m_tileProgramSwizzleAA;
269     OwnPtr<TileCheckerboardProgram> m_tileCheckerboardProgram;
270
271     OwnPtr<RenderSurfaceProgram> m_renderSurfaceProgram;
272     OwnPtr<RenderSurfaceProgramAA> m_renderSurfaceProgramAA;
273     OwnPtr<RenderSurfaceMaskProgram> m_renderSurfaceMaskProgram;
274     OwnPtr<RenderSurfaceMaskProgramAA> m_renderSurfaceMaskProgramAA;
275
276     OwnPtr<TextureProgram> m_textureProgram;
277     OwnPtr<TextureProgramFlip> m_textureProgramFlip;
278     OwnPtr<TextureIOSurfaceProgram> m_textureIOSurfaceProgram;
279
280     OwnPtr<VideoYUVProgram> m_videoYUVProgram;
281     OwnPtr<VideoStreamTextureProgram> m_videoStreamTextureProgram;
282
283     OwnPtr<SolidColorProgram> m_solidColorProgram;
284     OwnPtr<HeadsUpDisplayProgram> m_headsUpDisplayProgram;
285
286     OwnPtr<TextureManager> m_renderSurfaceTextureManager;
287     OwnPtr<AcceleratedTextureCopier> m_textureCopier;
288     OwnPtr<AcceleratedTextureUploader> m_textureUploader;
289     OwnPtr<TrackingTextureAllocator> m_contentsTextureAllocator;
290     OwnPtr<TrackingTextureAllocator> m_renderSurfaceTextureAllocator;
291
292     RefPtr<GraphicsContext3D> m_context;
293
294     CCRenderSurface* m_defaultRenderSurface;
295
296     FloatQuad m_sharedGeometryQuad;
297
298     bool m_isViewportChanged;
299     bool m_isFramebufferDiscarded;
300 };
301
302
303 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
304 // call made by the compositor. Useful for debugging rendering issues but
305 // will significantly degrade performance.
306 #define DEBUG_GL_CALLS 0
307
308 #if DEBUG_GL_CALLS && !defined ( NDEBUG )
309 #define GLC(context, x) (x, LayerRendererChromium::debugGLCall(&*context, #x, __FILE__, __LINE__))
310 #else
311 #define GLC(context, x) (x)
312 #endif
313
314
315 }
316
317 #endif // USE(ACCELERATED_COMPOSITING)
318
319 #endif