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_OUTPUT_GL_RENDERER_H_
6 #define CC_OUTPUT_GL_RENDERER_H_
8 #include "cc/base/cc_export.h"
9 #include "cc/output/direct_renderer.h"
10 #include "cc/output/gl_renderer_draw_cache.h"
11 #include "cc/output/renderer.h"
12 #include "cc/quads/checkerboard_draw_quad.h"
13 #include "cc/quads/debug_border_draw_quad.h"
14 #include "cc/quads/io_surface_draw_quad.h"
15 #include "cc/quads/render_pass_draw_quad.h"
16 #include "cc/quads/solid_color_draw_quad.h"
17 #include "cc/quads/tile_draw_quad.h"
18 #include "cc/quads/yuv_video_draw_quad.h"
19 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
20 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsMemoryAllocation.h"
21 #include "ui/gfx/quad_f.h"
25 class GLRendererShaderTest
;
27 class PictureDrawQuad
;
29 class StreamVideoDrawQuad
;
30 class TextureDrawQuad
;
31 class GeometryBinding
;
32 class ScopedEnsureFramebufferAllocation
;
34 // Class that handles drawing of composited render layers using GL.
35 class CC_EXPORT GLRenderer
36 : public DirectRenderer
,
37 public NON_EXPORTED_BASE(
38 WebKit::WebGraphicsContext3D::
39 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM
) {
41 static scoped_ptr
<GLRenderer
> Create(RendererClient
* client
,
42 OutputSurface
* output_surface
,
43 ResourceProvider
* resource_provider
,
44 int highp_threshold_min
);
46 virtual ~GLRenderer();
48 virtual const RendererCapabilities
& Capabilities() const OVERRIDE
;
50 WebKit::WebGraphicsContext3D
* Context();
52 virtual void ViewportChanged() OVERRIDE
;
54 // Waits for rendering to finish.
55 virtual void Finish() OVERRIDE
;
57 virtual void DoNoOp() OVERRIDE
;
58 // Puts backbuffer onscreen.
59 virtual bool SwapBuffers(const LatencyInfo
& latency_info
) OVERRIDE
;
61 virtual void GetFramebufferPixels(void* pixels
, gfx::Rect rect
) OVERRIDE
;
63 virtual bool IsContextLost() OVERRIDE
;
65 virtual void SetVisible(bool visible
) OVERRIDE
;
67 virtual void SendManagedMemoryStats(size_t bytes_visible
,
68 size_t bytes_visible_and_nearby
,
69 size_t bytes_allocated
) OVERRIDE
;
71 static void DebugGLCall(WebKit::WebGraphicsContext3D
* context
,
77 GLRenderer(RendererClient
* client
,
78 OutputSurface
* output_surface
,
79 ResourceProvider
* resource_provider
,
80 int highp_threshold_min
);
82 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_
; }
85 const gfx::QuadF
& SharedGeometryQuad() const { return shared_geometry_quad_
; }
86 const GeometryBinding
* SharedGeometry() const {
87 return shared_geometry_
.get();
90 bool GetFramebufferTexture(ScopedResource
* resource
, gfx::Rect device_rect
);
91 void ReleaseRenderPassTextures();
93 virtual void BindFramebufferToOutputSurface(DrawingFrame
* frame
) OVERRIDE
;
94 virtual bool BindFramebufferToTexture(DrawingFrame
* frame
,
95 const ScopedResource
* resource
,
96 gfx::Rect framebuffer_rect
) OVERRIDE
;
97 virtual void SetDrawViewportSize(gfx::Size viewport_size
) OVERRIDE
;
98 virtual void SetScissorTestRect(gfx::Rect scissor_rect
) OVERRIDE
;
99 virtual void ClearFramebuffer(DrawingFrame
* frame
) OVERRIDE
;
100 virtual void DoDrawQuad(DrawingFrame
* frame
, const class DrawQuad
*) OVERRIDE
;
101 virtual void BeginDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
102 virtual void FinishDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
103 virtual bool FlippedFramebuffer() const OVERRIDE
;
104 virtual void EnsureScissorTestEnabled() OVERRIDE
;
105 virtual void EnsureScissorTestDisabled() OVERRIDE
;
106 virtual void CopyCurrentRenderPassToBitmap(DrawingFrame
* frame
,
107 SkBitmap
* bitmap
) OVERRIDE
;
108 virtual void FinishDrawingQuadList() OVERRIDE
;
111 friend class GLRendererShaderPixelTest
;
112 friend class GLRendererShaderTest
;
114 static void ToGLMatrix(float* gl_matrix
, const gfx::Transform
& transform
);
115 static ManagedMemoryPolicy::PriorityCutoff
PriorityCutoff(
116 WebKit::WebGraphicsMemoryAllocation::PriorityCutoff priority_cutoff
);
118 void DrawCheckerboardQuad(const DrawingFrame
* frame
,
119 const CheckerboardDrawQuad
* quad
);
120 void DrawDebugBorderQuad(const DrawingFrame
* frame
,
121 const DebugBorderDrawQuad
* quad
);
122 scoped_ptr
<ScopedResource
> DrawBackgroundFilters(
124 const RenderPassDrawQuad
* quad
,
125 const gfx::Transform
& contents_device_transform
,
126 const gfx::Transform
& contents_device_transformInverse
);
127 void DrawRenderPassQuad(DrawingFrame
* frame
, const RenderPassDrawQuad
* quad
);
128 void DrawSolidColorQuad(const DrawingFrame
* frame
,
129 const SolidColorDrawQuad
* quad
);
130 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
131 const StreamVideoDrawQuad
* quad
);
132 void DrawTextureQuad(const DrawingFrame
* frame
, const TextureDrawQuad
* quad
);
133 void EnqueueTextureQuad(const DrawingFrame
* frame
,
134 const TextureDrawQuad
* quad
);
135 void FlushTextureQuadCache();
136 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
137 const IOSurfaceDrawQuad
* quad
);
138 void DrawTileQuad(const DrawingFrame
* frame
, const TileDrawQuad
* quad
);
139 void DrawContentQuad(const DrawingFrame
* frame
,
140 const ContentDrawQuadBase
* quad
,
141 ResourceProvider::ResourceId resource_id
);
142 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
143 const YUVVideoDrawQuad
* quad
);
144 void DrawPictureQuad(const DrawingFrame
* frame
,
145 const PictureDrawQuad
* quad
);
147 void SetShaderOpacity(float opacity
, int alpha_location
);
148 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
149 void DrawQuadGeometry(const DrawingFrame
* frame
,
150 const gfx::Transform
& draw_transform
,
151 const gfx::RectF
& quad_rect
,
152 int matrix_location
);
153 void SetBlendEnabled(bool enabled
);
154 bool blend_enabled() const { return blend_shadow_
; }
155 void SetUseProgram(unsigned program
);
157 void CopyTextureToFramebuffer(const DrawingFrame
* frame
,
160 const gfx::Transform
& draw_matrix
);
162 // Check if quad needs antialiasing and if so, inflate the quad and
163 // fill edge array for fragment shader. local_quad is set to
164 // inflated quad if antialiasing is required, otherwise it is left
165 // unchanged. edge array is filled with inflated quad's edge data
166 // if antialiasing is required, otherwise it is left unchanged.
167 // Returns true if quad requires antialiasing and false otherwise.
168 bool SetupQuadForAntialiasing(const gfx::Transform
& device_transform
,
169 const DrawQuad
* quad
,
170 gfx::QuadF
* local_quad
,
171 float edge
[24]) const;
173 bool UseScopedTexture(DrawingFrame
* frame
,
174 const ScopedResource
* resource
,
175 gfx::Rect viewport_rect
);
177 bool MakeContextCurrent();
179 bool InitializeSharedObjects();
180 void CleanupSharedObjects();
183 // WebGraphicsContext3D::WebGraphicsMemoryAllocationChangedCallbackCHROMIUM
185 virtual void onMemoryAllocationChanged(
186 WebKit::WebGraphicsMemoryAllocation allocation
) OVERRIDE
;
187 void DiscardBackbuffer();
188 void EnsureBackbuffer();
189 void EnforceMemoryPolicy();
191 RendererCapabilities capabilities_
;
193 unsigned offscreen_framebuffer_id_
;
195 scoped_ptr
<GeometryBinding
> shared_geometry_
;
196 gfx::QuadF shared_geometry_quad_
;
198 // This block of bindings defines all of the programs used by the compositor
199 // itself. Add any new programs here to GLRendererShaderTest.
201 // Tiled layer shaders.
202 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
204 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexClampAlphaAA
>
206 typedef ProgramBinding
<VertexShaderTile
,
207 FragmentShaderRGBATexClampSwizzleAlphaAA
>
208 TileProgramSwizzleAA
;
209 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
211 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
213 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
214 TileProgramSwizzleOpaque
;
215 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
216 TileCheckerboardProgram
;
219 typedef ProgramBinding
<VertexShaderPosTexTransform
,
220 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
221 typedef ProgramBinding
<VertexShaderPosTexTransformFlip
,
222 FragmentShaderRGBATexVaryingAlpha
> TextureProgramFlip
;
223 typedef ProgramBinding
<VertexShaderPosTexTransform
,
224 FragmentShaderRGBATexRectVaryingAlpha
>
225 TextureIOSurfaceProgram
;
227 // Render surface shaders.
228 typedef ProgramBinding
<VertexShaderPosTexTransform
,
229 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
230 typedef ProgramBinding
<VertexShaderPosTexTransform
,
231 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
232 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderRGBATexAlphaAA
>
234 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderRGBATexAlphaMaskAA
>
235 RenderPassMaskProgramAA
;
236 typedef ProgramBinding
<VertexShaderPosTexTransform
,
237 FragmentShaderRGBATexColorMatrixAlpha
>
238 RenderPassColorMatrixProgram
;
239 typedef ProgramBinding
<VertexShaderQuad
,
240 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
241 RenderPassMaskColorMatrixProgramAA
;
242 typedef ProgramBinding
<VertexShaderQuad
,
243 FragmentShaderRGBATexAlphaColorMatrixAA
>
244 RenderPassColorMatrixProgramAA
;
245 typedef ProgramBinding
<VertexShaderPosTexTransform
,
246 FragmentShaderRGBATexAlphaMaskColorMatrix
>
247 RenderPassMaskColorMatrixProgram
;
250 typedef ProgramBinding
<VertexShaderVideoTransform
,
251 FragmentShaderOESImageExternal
>
252 VideoStreamTextureProgram
;
253 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVVideo
>
256 // Special purpose / effects shaders.
257 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
259 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
261 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColorAA
>
264 const TileProgram
* GetTileProgram(TexCoordPrecision precision
);
265 const TileProgramOpaque
* GetTileProgramOpaque(TexCoordPrecision precision
);
266 const TileProgramAA
* GetTileProgramAA(TexCoordPrecision precision
);
267 const TileProgramSwizzle
* GetTileProgramSwizzle(TexCoordPrecision precision
);
268 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
269 TexCoordPrecision precision
);
270 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
271 TexCoordPrecision precision
);
272 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
274 const RenderPassProgram
* GetRenderPassProgram(
275 TexCoordPrecision precision
);
276 const RenderPassProgramAA
* GetRenderPassProgramAA(
277 TexCoordPrecision precision
);
278 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
279 TexCoordPrecision precision
);
280 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
281 TexCoordPrecision precision
);
282 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
283 TexCoordPrecision precision
);
284 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
285 TexCoordPrecision precision
);
286 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
287 TexCoordPrecision precision
);
288 const RenderPassMaskColorMatrixProgramAA
*
289 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
);
291 const TextureProgram
* GetTextureProgram(
292 TexCoordPrecision precision
);
293 const TextureProgramFlip
* GetTextureProgramFlip(
294 TexCoordPrecision precision
);
295 const TextureIOSurfaceProgram
* GetTextureIOSurfaceProgram(
296 TexCoordPrecision precision
);
298 const VideoYUVProgram
* GetVideoYUVProgram(
299 TexCoordPrecision precision
);
300 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
301 TexCoordPrecision precision
);
303 const DebugBorderProgram
* GetDebugBorderProgram();
304 const SolidColorProgram
* GetSolidColorProgram();
305 const SolidColorProgramAA
* GetSolidColorProgramAA();
307 scoped_ptr
<TileProgram
> tile_program_
;
308 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_
;
309 scoped_ptr
<TileProgramAA
> tile_program_aa_
;
310 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_
;
311 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_
;
312 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_
;
313 scoped_ptr
<TileCheckerboardProgram
> tile_checkerboard_program_
;
315 scoped_ptr
<TileProgram
> tile_program_highp_
;
316 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_highp_
;
317 scoped_ptr
<TileProgramAA
> tile_program_aa_highp_
;
318 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_highp_
;
319 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_highp_
;
320 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_highp_
;
322 scoped_ptr
<TextureProgram
> texture_program_
;
323 scoped_ptr
<TextureProgramFlip
> texture_program_flip_
;
324 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_
;
326 scoped_ptr
<TextureProgram
> texture_program_highp_
;
327 scoped_ptr
<TextureProgramFlip
> texture_program_flip_highp_
;
328 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_highp_
;
330 scoped_ptr
<RenderPassProgram
> render_pass_program_
;
331 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_
;
332 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_
;
333 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_
;
334 scoped_ptr
<RenderPassColorMatrixProgram
> render_pass_color_matrix_program_
;
335 scoped_ptr
<RenderPassColorMatrixProgramAA
>
336 render_pass_color_matrix_program_aa_
;
337 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
338 render_pass_mask_color_matrix_program_
;
339 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
340 render_pass_mask_color_matrix_program_aa_
;
342 scoped_ptr
<RenderPassProgram
> render_pass_program_highp_
;
343 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_highp_
;
344 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_highp_
;
345 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_highp_
;
346 scoped_ptr
<RenderPassColorMatrixProgram
>
347 render_pass_color_matrix_program_highp_
;
348 scoped_ptr
<RenderPassColorMatrixProgramAA
>
349 render_pass_color_matrix_program_aa_highp_
;
350 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
351 render_pass_mask_color_matrix_program_highp_
;
352 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
353 render_pass_mask_color_matrix_program_aa_highp_
;
355 scoped_ptr
<VideoYUVProgram
> video_yuv_program_
;
356 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_
;
358 scoped_ptr
<VideoYUVProgram
> video_yuv_program_highp_
;
359 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_highp_
;
361 scoped_ptr
<DebugBorderProgram
> debug_border_program_
;
362 scoped_ptr
<SolidColorProgram
> solid_color_program_
;
363 scoped_ptr
<SolidColorProgramAA
> solid_color_program_aa_
;
365 OutputSurface
* output_surface_
;
366 WebKit::WebGraphicsContext3D
* context_
;
368 gfx::Rect swap_buffer_rect_
;
369 gfx::Rect scissor_rect_
;
370 bool is_viewport_changed_
;
371 bool is_backbuffer_discarded_
;
372 bool discard_backbuffer_when_not_visible_
;
373 bool is_using_bind_uniform_
;
375 bool is_scissor_enabled_
;
377 unsigned program_shadow_
;
378 TexturedQuadDrawCache draw_cache_
;
379 int highp_threshold_min_
;
381 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
383 scoped_refptr
<ResourceProvider::Fence
> last_swap_fence_
;
385 SkBitmap on_demand_tile_raster_bitmap_
;
386 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
388 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
391 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
392 // call made by the compositor. Useful for debugging rendering issues but
393 // will significantly degrade performance.
394 #define DEBUG_GL_CALLS 0
396 #if DEBUG_GL_CALLS && !defined(NDEBUG)
397 #define GLC(context, x) \
398 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
400 #define GLC(context, x) (x)
405 #endif // CC_OUTPUT_GL_RENDERER_H_