[chromium] LayerRendererChromium is not getting visibility messages in single threade...
[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 "Extensions3DChromium.h"
38 #include "TextureCopier.h"
39 #include "TrackingTextureAllocator.h"
40 #include "cc/CCRenderer.h"
41 #include <wtf/PassOwnPtr.h>
42
43 namespace WebCore {
44
45 class CCCheckerboardDrawQuad;
46 class CCDebugBorderDrawQuad;
47 class CCDrawQuad;
48 class CCIOSurfaceDrawQuad;
49 class CCRenderPassDrawQuad;
50 class CCSolidColorDrawQuad;
51 class CCStreamVideoDrawQuad;
52 class CCTextureDrawQuad;
53 class CCTileDrawQuad;
54 class CCYUVVideoDrawQuad;
55 class GeometryBinding;
56 class GraphicsContext3D;
57 class LayerRendererSwapBuffersCompleteCallbackAdapter;
58 class ManagedTexture;
59 class ScopedEnsureFramebufferAllocation;
60
61 // Class that handles drawing of composited render layers using GL.
62 class LayerRendererChromium : public CCRenderer {
63     WTF_MAKE_NONCOPYABLE(LayerRendererChromium);
64 public:
65     static PassOwnPtr<LayerRendererChromium> create(CCRendererClient*, PassRefPtr<GraphicsContext3D>, TextureUploaderOption);
66
67     virtual ~LayerRendererChromium();
68
69     virtual const LayerRendererCapabilities& capabilities() const OVERRIDE { return m_capabilities; }
70
71     GraphicsContext3D* context();
72
73     virtual void viewportChanged() OVERRIDE;
74
75     const FloatQuad& sharedGeometryQuad() const { return m_sharedGeometryQuad; }
76
77     virtual void decideRenderPassAllocationsForFrame(const CCRenderPassList&) OVERRIDE;
78     virtual void beginDrawingFrame(const CCRenderPass* defaultRenderPass) OVERRIDE;
79     virtual void drawRenderPass(const CCRenderPass*, const FloatRect& framebufferDamageRect) OVERRIDE;
80     virtual void finishDrawingFrame() OVERRIDE;
81
82     virtual void drawHeadsUpDisplay(ManagedTexture*, const IntSize& hudSize) OVERRIDE;
83
84     // waits for rendering to finish
85     virtual void finish() OVERRIDE;
86
87     virtual void doNoOp() OVERRIDE;
88     // puts backbuffer onscreen
89     virtual bool swapBuffers(const IntRect& subBuffer) OVERRIDE;
90
91     static void debugGLCall(GraphicsContext3D*, const char* command, const char* file, int line);
92
93     const GeometryBinding* sharedGeometry() const { return m_sharedGeometry.get(); }
94
95     virtual void getFramebufferPixels(void *pixels, const IntRect&) OVERRIDE;
96     bool getFramebufferTexture(ManagedTexture*, const IntRect& deviceRect);
97
98     virtual TextureManager* implTextureManager() const OVERRIDE { return m_implTextureManager.get(); }
99     virtual TextureCopier* textureCopier() const OVERRIDE { return m_textureCopier.get(); }
100     virtual TextureUploader* textureUploader() const OVERRIDE { return m_textureUploader.get(); }
101     virtual TextureAllocator* implTextureAllocator() const OVERRIDE { return m_implTextureAllocator.get(); }
102     virtual TextureAllocator* contentsTextureAllocator() const OVERRIDE { return m_contentsTextureAllocator.get(); }
103
104     virtual void setScissorToRect(const IntRect&) OVERRIDE;
105
106     virtual bool isContextLost() OVERRIDE;
107
108     virtual void setVisible(bool) OVERRIDE;
109
110     void drawTexturedQuad(const WebKit::WebTransformationMatrix& layerMatrix,
111                           float width, float height, float opacity, const FloatQuad&,
112                           int matrixLocation, int alphaLocation, int quadLocation);
113     void copyTextureToFramebuffer(int textureId, const IntSize& bounds, const WebKit::WebTransformationMatrix& drawMatrix);
114     void setGpuMemoryAllocation(Extensions3DChromium::GpuMemoryAllocationCHROMIUM);
115
116 protected:
117     void discardFramebuffer();
118     void ensureFramebuffer();
119     bool isFramebufferDiscarded() const { return m_isFramebufferDiscarded; }
120
121     LayerRendererChromium(CCRendererClient*, PassRefPtr<GraphicsContext3D>, TextureUploaderOption);
122     bool initialize();
123
124 private:
125     static void toGLMatrix(float*, const WebKit::WebTransformationMatrix&);
126
127     void drawQuad(const CCDrawQuad*);
128     void drawCheckerboardQuad(const CCCheckerboardDrawQuad*);
129     void drawDebugBorderQuad(const CCDebugBorderDrawQuad*);
130     void drawBackgroundFilters(const CCRenderPassDrawQuad*, const WebKit::WebTransformationMatrix& deviceTransform);
131     void drawRenderPassQuad(const CCRenderPassDrawQuad*);
132     void drawSolidColorQuad(const CCSolidColorDrawQuad*);
133     void drawStreamVideoQuad(const CCStreamVideoDrawQuad*);
134     void drawTextureQuad(const CCTextureDrawQuad*);
135     void drawIOSurfaceQuad(const CCIOSurfaceDrawQuad*);
136     void drawTileQuad(const CCTileDrawQuad*);
137     void drawYUVVideoQuad(const CCYUVVideoDrawQuad*);
138
139     void setDrawFramebufferRect(const IntRect&, bool flipY);
140
141     // The current drawing target is either a RenderPass or ManagedTexture. Use these functions to switch to a new drawing target.
142     bool useRenderPass(const CCRenderPass*);
143     bool useManagedTexture(ManagedTexture*, const IntRect& viewportRect);
144     bool isCurrentRenderPass(const CCRenderPass*);
145
146     bool bindFramebufferToTexture(ManagedTexture*, const IntRect& viewportRect);
147
148     void clearRenderPass(const CCRenderPass*, const FloatRect& framebufferDamageRect);
149
150     void releaseRenderPassTextures();
151
152     bool makeContextCurrent();
153
154     bool initializeSharedObjects();
155     void cleanupSharedObjects();
156
157     friend class LayerRendererSwapBuffersCompleteCallbackAdapter;
158     void onSwapBuffersComplete();
159
160     LayerRendererCapabilities m_capabilities;
161
162     const CCRenderPass* m_currentRenderPass;
163     ManagedTexture* m_currentManagedTexture;
164     unsigned m_offscreenFramebufferId;
165
166     OwnPtr<GeometryBinding> m_sharedGeometry;
167     FloatQuad m_sharedGeometryQuad;
168
169     // This block of bindings defines all of the programs used by the compositor itself.
170
171     // Tiled layer shaders.
172     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha> TileProgram;
173     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexClampAlphaAA> TileProgramAA;
174     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexClampSwizzleAlphaAA> TileProgramSwizzleAA;
175     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque> TileProgramOpaque;
176     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha> TileProgramSwizzle;
177     typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque> TileProgramSwizzleOpaque;
178     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard> TileCheckerboardProgram;
179
180     // Render surface shaders.
181     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexAlpha> RenderPassProgram;
182     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexAlphaMask> RenderPassMaskProgram;
183     typedef ProgramBinding<VertexShaderQuad, FragmentShaderRGBATexAlphaAA> RenderPassProgramAA;
184     typedef ProgramBinding<VertexShaderQuad, FragmentShaderRGBATexAlphaMaskAA> RenderPassMaskProgramAA;
185
186     // Texture shaders.
187     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexAlpha> TextureProgram;
188     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexFlipAlpha> TextureProgramFlip;
189     typedef ProgramBinding<VertexShaderPosTexTransform, FragmentShaderRGBATexRectFlipAlpha> TextureIOSurfaceProgram;
190
191     // Video shaders.
192     typedef ProgramBinding<VertexShaderVideoTransform, FragmentShaderOESImageExternal> VideoStreamTextureProgram;
193     typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo> VideoYUVProgram;
194
195     // Special purpose / effects shaders.
196     typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> SolidColorProgram;
197
198     // Debugging shaders.
199     typedef ProgramBinding<VertexShaderPosTex, FragmentShaderRGBATexSwizzleAlpha> HeadsUpDisplayProgram;
200
201
202     const TileProgram* tileProgram();
203     const TileProgramOpaque* tileProgramOpaque();
204     const TileProgramAA* tileProgramAA();
205     const TileProgramSwizzle* tileProgramSwizzle();
206     const TileProgramSwizzleOpaque* tileProgramSwizzleOpaque();
207     const TileProgramSwizzleAA* tileProgramSwizzleAA();
208     const TileCheckerboardProgram* tileCheckerboardProgram();
209
210     const RenderPassProgram* renderPassProgram();
211     const RenderPassProgramAA* renderPassProgramAA();
212     const RenderPassMaskProgram* renderPassMaskProgram();
213     const RenderPassMaskProgramAA* renderPassMaskProgramAA();
214
215     const TextureProgram* textureProgram();
216     const TextureProgramFlip* textureProgramFlip();
217     const TextureIOSurfaceProgram* textureIOSurfaceProgram();
218
219     const VideoYUVProgram* videoYUVProgram();
220     const VideoStreamTextureProgram* videoStreamTextureProgram();
221
222     const SolidColorProgram* solidColorProgram();
223
224     const HeadsUpDisplayProgram* headsUpDisplayProgram();
225
226     OwnPtr<TileProgram> m_tileProgram;
227     OwnPtr<TileProgramOpaque> m_tileProgramOpaque;
228     OwnPtr<TileProgramAA> m_tileProgramAA;
229     OwnPtr<TileProgramSwizzle> m_tileProgramSwizzle;
230     OwnPtr<TileProgramSwizzleOpaque> m_tileProgramSwizzleOpaque;
231     OwnPtr<TileProgramSwizzleAA> m_tileProgramSwizzleAA;
232     OwnPtr<TileCheckerboardProgram> m_tileCheckerboardProgram;
233
234     OwnPtr<RenderPassProgram> m_renderPassProgram;
235     OwnPtr<RenderPassProgramAA> m_renderPassProgramAA;
236     OwnPtr<RenderPassMaskProgram> m_renderPassMaskProgram;
237     OwnPtr<RenderPassMaskProgramAA> m_renderPassMaskProgramAA;
238
239     OwnPtr<TextureProgram> m_textureProgram;
240     OwnPtr<TextureProgramFlip> m_textureProgramFlip;
241     OwnPtr<TextureIOSurfaceProgram> m_textureIOSurfaceProgram;
242
243     OwnPtr<VideoYUVProgram> m_videoYUVProgram;
244     OwnPtr<VideoStreamTextureProgram> m_videoStreamTextureProgram;
245
246     OwnPtr<SolidColorProgram> m_solidColorProgram;
247     OwnPtr<HeadsUpDisplayProgram> m_headsUpDisplayProgram;
248
249     OwnPtr<TextureManager> m_implTextureManager;
250     OwnPtr<AcceleratedTextureCopier> m_textureCopier;
251     OwnPtr<TextureUploader> m_textureUploader;
252     OwnPtr<TrackingTextureAllocator> m_contentsTextureAllocator;
253     OwnPtr<TrackingTextureAllocator> m_implTextureAllocator;
254
255     RefPtr<GraphicsContext3D> m_context;
256
257     const CCRenderPass* m_defaultRenderPass;
258
259     bool m_isViewportChanged;
260     bool m_isFramebufferDiscarded;
261     bool m_visible;
262     TextureUploaderOption m_textureUploaderSetting;
263 };
264
265
266 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
267 // call made by the compositor. Useful for debugging rendering issues but
268 // will significantly degrade performance.
269 #define DEBUG_GL_CALLS 0
270
271 #if DEBUG_GL_CALLS && !defined ( NDEBUG )
272 #define GLC(context, x) (x, LayerRendererChromium::debugGLCall(&*context, #x, __FILE__, __LINE__))
273 #else
274 #define GLC(context, x) (x)
275 #endif
276
277
278 }
279
280 #endif // USE(ACCELERATED_COMPOSITING)
281
282 #endif