1 // Copyright 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_GL_RENDERER_H_
6 #define CC_GL_RENDERER_H_
8 #include "cc/cc_export.h"
9 #include "cc/checkerboard_draw_quad.h"
10 #include "cc/debug_border_draw_quad.h"
11 #include "cc/direct_renderer.h"
12 #include "cc/gl_renderer_draw_cache.h"
13 #include "cc/io_surface_draw_quad.h"
14 #include "cc/render_pass_draw_quad.h"
15 #include "cc/renderer.h"
16 #include "cc/solid_color_draw_quad.h"
17 #include "cc/tile_draw_quad.h"
18 #include "cc/yuv_video_draw_quad.h"
19 #include "ui/gfx/quad_f.h"
22 class WebGraphicsContext3D
;
28 class StreamVideoDrawQuad
;
29 class TextureDrawQuad
;
30 class GeometryBinding
;
31 class ScopedEnsureFramebufferAllocation
;
33 // Class that handles drawing of composited render layers using GL.
34 class CC_EXPORT GLRenderer
: public DirectRenderer
,
35 public NON_EXPORTED_BASE(WebKit::WebGraphicsContext3D::WebGraphicsSwapBuffersCompleteCallbackCHROMIUM
),
36 public NON_EXPORTED_BASE(WebKit::WebGraphicsContext3D::WebGraphicsMemoryAllocationChangedCallbackCHROMIUM
),
37 public NON_EXPORTED_BASE(WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback
) {
39 static scoped_ptr
<GLRenderer
> create(RendererClient
*, ResourceProvider
*);
41 virtual ~GLRenderer();
43 virtual const RendererCapabilities
& capabilities() const OVERRIDE
;
45 WebKit::WebGraphicsContext3D
* context();
47 virtual void viewportChanged() OVERRIDE
;
49 // waits for rendering to finish
50 virtual void finish() OVERRIDE
;
52 virtual void doNoOp() OVERRIDE
;
53 // puts backbuffer onscreen
54 virtual bool swapBuffers() OVERRIDE
;
56 virtual void getFramebufferPixels(void *pixels
, const gfx::Rect
&) OVERRIDE
;
58 virtual bool isContextLost() OVERRIDE
;
60 virtual void setVisible(bool) OVERRIDE
;
62 virtual void sendManagedMemoryStats(size_t bytesVisible
, size_t bytesVisibleAndNearby
, size_t bytesAllocated
) OVERRIDE
;
65 GLRenderer(RendererClient
*, ResourceProvider
*);
67 static void debugGLCall(WebKit::WebGraphicsContext3D
*, const char* command
, const char* file
, int line
);
69 bool isFramebufferDiscarded() const { return m_isFramebufferDiscarded
; }
72 const gfx::QuadF
& sharedGeometryQuad() const { return m_sharedGeometryQuad
; }
73 const GeometryBinding
* sharedGeometry() const { return m_sharedGeometry
.get(); }
75 bool getFramebufferTexture(ScopedResource
*, const gfx::Rect
& deviceRect
);
76 void releaseRenderPassTextures();
78 virtual void bindFramebufferToOutputSurface(DrawingFrame
&) OVERRIDE
;
79 virtual bool bindFramebufferToTexture(DrawingFrame
&, const ScopedResource
*, const gfx::Rect
& framebufferRect
) OVERRIDE
;
80 virtual void setDrawViewportSize(const gfx::Size
&) OVERRIDE
;
81 virtual void setScissorTestRect(const gfx::Rect
& scissorRect
) OVERRIDE
;
82 virtual void clearFramebuffer(DrawingFrame
&) OVERRIDE
;
83 virtual void drawQuad(DrawingFrame
&, const DrawQuad
*) OVERRIDE
;
84 virtual void beginDrawingFrame(DrawingFrame
&) OVERRIDE
;
85 virtual void finishDrawingFrame(DrawingFrame
&) OVERRIDE
;
86 virtual bool flippedFramebuffer() const OVERRIDE
;
87 virtual void ensureScissorTestEnabled() OVERRIDE
;
88 virtual void ensureScissorTestDisabled() OVERRIDE
;
89 virtual void finishDrawingQuadList() OVERRIDE
;
92 static void toGLMatrix(float*, const gfx::Transform
&);
93 static int priorityCutoffValue(WebKit::WebGraphicsMemoryAllocation::PriorityCutoff
);
95 void drawCheckerboardQuad(const DrawingFrame
&, const CheckerboardDrawQuad
*);
96 void drawDebugBorderQuad(const DrawingFrame
&, const DebugBorderDrawQuad
*);
97 scoped_ptr
<ScopedResource
> drawBackgroundFilters(
98 DrawingFrame
&, const RenderPassDrawQuad
*, const WebKit::WebFilterOperations
&,
99 const gfx::Transform
& contentsDeviceTransform
,
100 const gfx::Transform
& contentsDeviceTransformInverse
);
101 void drawRenderPassQuad(DrawingFrame
&, const RenderPassDrawQuad
*);
102 void drawSolidColorQuad(const DrawingFrame
&, const SolidColorDrawQuad
*);
103 void drawStreamVideoQuad(const DrawingFrame
&, const StreamVideoDrawQuad
*);
104 void drawTextureQuad(const DrawingFrame
&, const TextureDrawQuad
*);
105 void enqueueTextureQuad(const DrawingFrame
&, const TextureDrawQuad
*);
106 void flushTextureQuadCache();
107 void drawIOSurfaceQuad(const DrawingFrame
&, const IOSurfaceDrawQuad
*);
108 void drawTileQuad(const DrawingFrame
&, const TileDrawQuad
*);
109 void drawYUVVideoQuad(const DrawingFrame
&, const YUVVideoDrawQuad
*);
111 void setShaderOpacity(float opacity
, int alphaLocation
);
112 void setShaderQuadF(const gfx::QuadF
&, int quadLocation
);
113 void drawQuadGeometry(const DrawingFrame
&, const gfx::Transform
& drawTransform
, const gfx::RectF
& quadRect
, int matrixLocation
);
114 void setBlendEnabled(bool enabled
);
115 void setUseProgram(unsigned program
);
117 void copyTextureToFramebuffer(const DrawingFrame
&, int textureId
, const gfx::Rect
&, const gfx::Transform
& drawMatrix
);
119 bool useScopedTexture(DrawingFrame
&, const ScopedResource
*, const gfx::Rect
& viewportRect
);
121 bool makeContextCurrent();
123 bool initializeSharedObjects();
124 void cleanupSharedObjects();
126 // WebKit::WebGraphicsContext3D::WebGraphicsSwapBuffersCompleteCallbackCHROMIUM implementation.
127 virtual void onSwapBuffersComplete() OVERRIDE
;
129 // WebKit::WebGraphicsContext3D::WebGraphicsMemoryAllocationChangedCallbackCHROMIUM implementation.
130 virtual void onMemoryAllocationChanged(WebKit::WebGraphicsMemoryAllocation
) OVERRIDE
;
131 void discardFramebuffer();
132 void ensureFramebuffer();
133 void enforceMemoryPolicy();
135 // WebGraphicsContext3D::WebGraphicsContextLostCallback implementation.
136 virtual void onContextLost() OVERRIDE
;
138 RendererCapabilities m_capabilities
;
140 unsigned m_offscreenFramebufferId
;
142 scoped_ptr
<GeometryBinding
> m_sharedGeometry
;
143 gfx::QuadF m_sharedGeometryQuad
;
145 // This block of bindings defines all of the programs used by the compositor itself.
147 // Tiled layer shaders.
148 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
> TileProgram
;
149 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexClampAlphaAA
> TileProgramAA
;
150 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexClampSwizzleAlphaAA
> TileProgramSwizzleAA
;
151 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
> TileProgramOpaque
;
152 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
> TileProgramSwizzle
;
153 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
> TileProgramSwizzleOpaque
;
154 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
> TileCheckerboardProgram
;
156 // Render surface shaders.
157 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderRGBATexAlpha
> RenderPassProgram
;
158 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
159 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
160 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderRGBATexAlphaMaskAA
> RenderPassMaskProgramAA
;
163 typedef ProgramBinding
<VertexShaderPosTexTransform
, FragmentShaderRGBATexAlpha
> TextureProgram
;
164 typedef ProgramBinding
<VertexShaderPosTexTransform
, FragmentShaderRGBATexFlipAlpha
> TextureProgramFlip
;
165 typedef ProgramBinding
<VertexShaderPosTexTransform
, FragmentShaderRGBATexRectAlpha
> TextureIOSurfaceProgram
;
168 typedef ProgramBinding
<VertexShaderVideoTransform
, FragmentShaderOESImageExternal
> VideoStreamTextureProgram
;
169 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVVideo
> VideoYUVProgram
;
171 // Special purpose / effects shaders.
172 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
> SolidColorProgram
;
174 const TileProgram
* tileProgram();
175 const TileProgramOpaque
* tileProgramOpaque();
176 const TileProgramAA
* tileProgramAA();
177 const TileProgramSwizzle
* tileProgramSwizzle();
178 const TileProgramSwizzleOpaque
* tileProgramSwizzleOpaque();
179 const TileProgramSwizzleAA
* tileProgramSwizzleAA();
180 const TileCheckerboardProgram
* tileCheckerboardProgram();
182 const RenderPassProgram
* renderPassProgram();
183 const RenderPassProgramAA
* renderPassProgramAA();
184 const RenderPassMaskProgram
* renderPassMaskProgram();
185 const RenderPassMaskProgramAA
* renderPassMaskProgramAA();
187 const TextureProgram
* textureProgram();
188 const TextureProgramFlip
* textureProgramFlip();
189 const TextureIOSurfaceProgram
* textureIOSurfaceProgram();
191 const VideoYUVProgram
* videoYUVProgram();
192 const VideoStreamTextureProgram
* videoStreamTextureProgram();
194 const SolidColorProgram
* solidColorProgram();
196 scoped_ptr
<TileProgram
> m_tileProgram
;
197 scoped_ptr
<TileProgramOpaque
> m_tileProgramOpaque
;
198 scoped_ptr
<TileProgramAA
> m_tileProgramAA
;
199 scoped_ptr
<TileProgramSwizzle
> m_tileProgramSwizzle
;
200 scoped_ptr
<TileProgramSwizzleOpaque
> m_tileProgramSwizzleOpaque
;
201 scoped_ptr
<TileProgramSwizzleAA
> m_tileProgramSwizzleAA
;
202 scoped_ptr
<TileCheckerboardProgram
> m_tileCheckerboardProgram
;
204 scoped_ptr
<RenderPassProgram
> m_renderPassProgram
;
205 scoped_ptr
<RenderPassProgramAA
> m_renderPassProgramAA
;
206 scoped_ptr
<RenderPassMaskProgram
> m_renderPassMaskProgram
;
207 scoped_ptr
<RenderPassMaskProgramAA
> m_renderPassMaskProgramAA
;
209 scoped_ptr
<TextureProgram
> m_textureProgram
;
210 scoped_ptr
<TextureProgramFlip
> m_textureProgramFlip
;
211 scoped_ptr
<TextureIOSurfaceProgram
> m_textureIOSurfaceProgram
;
213 scoped_ptr
<VideoYUVProgram
> m_videoYUVProgram
;
214 scoped_ptr
<VideoStreamTextureProgram
> m_videoStreamTextureProgram
;
216 scoped_ptr
<SolidColorProgram
> m_solidColorProgram
;
218 WebKit::WebGraphicsContext3D
* m_context
;
220 gfx::Rect m_swapBufferRect
;
221 gfx::Rect m_scissorRect
;
222 bool m_isViewportChanged
;
223 bool m_isFramebufferDiscarded
;
224 bool m_discardFramebufferWhenNotVisible
;
225 bool m_isUsingBindUniform
;
227 bool m_isScissorEnabled
;
229 unsigned m_programShadow
;
230 TexturedQuadDrawCache m_drawCache
;
232 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> m_currentFramebufferLock
;
234 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
238 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
239 // call made by the compositor. Useful for debugging rendering issues but
240 // will significantly degrade performance.
241 #define DEBUG_GL_CALLS 0
243 #if DEBUG_GL_CALLS && !defined(NDEBUG)
244 #define GLC(context, x) (x, GLRenderer::debugGLCall(&*context, #x, __FILE__, __LINE__))
246 #define GLC(context, x) (x)
252 #endif // CC_GL_RENDERER_H_