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 "base/cancelable_callback.h"
9 #include "cc/base/cc_export.h"
10 #include "cc/base/scoped_ptr_vector.h"
11 #include "cc/output/direct_renderer.h"
12 #include "cc/output/gl_renderer_draw_cache.h"
13 #include "cc/output/program_binding.h"
14 #include "cc/output/renderer.h"
15 #include "cc/quads/checkerboard_draw_quad.h"
16 #include "cc/quads/debug_border_draw_quad.h"
17 #include "cc/quads/io_surface_draw_quad.h"
18 #include "cc/quads/render_pass_draw_quad.h"
19 #include "cc/quads/solid_color_draw_quad.h"
20 #include "cc/quads/tile_draw_quad.h"
21 #include "cc/quads/yuv_video_draw_quad.h"
22 #include "ui/gfx/quad_f.h"
26 namespace WebKit
{ class WebGraphicsContext3D
; }
30 class GLRendererShaderTest
;
32 class PictureDrawQuad
;
34 class StreamVideoDrawQuad
;
35 class TextureDrawQuad
;
36 class GeometryBinding
;
37 class ScopedEnsureFramebufferAllocation
;
39 // Class that handles drawing of composited render layers using GL.
40 class CC_EXPORT GLRenderer
: public DirectRenderer
{
42 static scoped_ptr
<GLRenderer
> Create(RendererClient
* client
,
43 OutputSurface
* output_surface
,
44 ResourceProvider
* resource_provider
,
45 int highp_threshold_min
,
46 bool use_skia_gpu_backend
);
48 virtual ~GLRenderer();
50 virtual const RendererCapabilities
& Capabilities() const OVERRIDE
;
52 WebKit::WebGraphicsContext3D
* Context();
54 virtual void ViewportChanged() OVERRIDE
;
56 // Waits for rendering to finish.
57 virtual void Finish() OVERRIDE
;
59 virtual void DoNoOp() OVERRIDE
;
60 virtual void SwapBuffers() OVERRIDE
;
62 virtual void GetFramebufferPixels(void* pixels
, gfx::Rect rect
) OVERRIDE
;
64 virtual bool IsContextLost() OVERRIDE
;
66 virtual void SetVisible(bool visible
) OVERRIDE
;
68 virtual void SendManagedMemoryStats(size_t bytes_visible
,
69 size_t bytes_visible_and_nearby
,
70 size_t bytes_allocated
) OVERRIDE
;
72 virtual void SetDiscardBackBufferWhenNotVisible(bool discard
) OVERRIDE
;
74 static void DebugGLCall(WebKit::WebGraphicsContext3D
* context
,
79 bool CanUseSkiaGPUBackend() const;
80 void LazyLabelOffscreenContext(ContextProvider
* offscreen_context_provider
);
83 GLRenderer(RendererClient
* client
,
84 OutputSurface
* output_surface
,
85 ResourceProvider
* resource_provider
,
86 int highp_threshold_min
);
88 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_
; }
90 void InitializeGrContext();
92 const gfx::QuadF
& SharedGeometryQuad() const { return shared_geometry_quad_
; }
93 const GeometryBinding
* SharedGeometry() const {
94 return shared_geometry_
.get();
97 void GetFramebufferPixelsAsync(gfx::Rect rect
,
98 scoped_ptr
<CopyOutputRequest
> request
);
99 void GetFramebufferTexture(unsigned texture_id
,
100 unsigned texture_format
,
101 gfx::Rect device_rect
);
102 void ReleaseRenderPassTextures();
104 void SetStencilEnabled(bool enabled
);
105 bool stencil_enabled() const { return stencil_shadow_
; }
106 void SetBlendEnabled(bool enabled
);
107 bool blend_enabled() const { return blend_shadow_
; }
109 virtual void BindFramebufferToOutputSurface(DrawingFrame
* frame
) OVERRIDE
;
110 virtual bool BindFramebufferToTexture(DrawingFrame
* frame
,
111 const ScopedResource
* resource
,
112 gfx::Rect target_rect
) OVERRIDE
;
113 virtual void SetDrawViewport(gfx::Rect window_space_viewport
) OVERRIDE
;
114 virtual void SetScissorTestRect(gfx::Rect scissor_rect
) OVERRIDE
;
115 virtual void ClearFramebuffer(DrawingFrame
* frame
) OVERRIDE
;
116 virtual void DoDrawQuad(DrawingFrame
* frame
, const class DrawQuad
*) OVERRIDE
;
117 virtual void BeginDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
118 virtual void FinishDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
119 virtual bool FlippedFramebuffer() const OVERRIDE
;
120 virtual void EnsureScissorTestEnabled() OVERRIDE
;
121 virtual void EnsureScissorTestDisabled() OVERRIDE
;
122 virtual void CopyCurrentRenderPassToBitmap(
124 scoped_ptr
<CopyOutputRequest
> request
) OVERRIDE
;
125 virtual void FinishDrawingQuadList() OVERRIDE
;
128 friend class GLRendererShaderPixelTest
;
129 friend class GLRendererShaderTest
;
131 static void ToGLMatrix(float* gl_matrix
, const gfx::Transform
& transform
);
133 void DrawCheckerboardQuad(const DrawingFrame
* frame
,
134 const CheckerboardDrawQuad
* quad
);
135 void DrawDebugBorderQuad(const DrawingFrame
* frame
,
136 const DebugBorderDrawQuad
* quad
);
137 scoped_ptr
<ScopedResource
> DrawBackgroundFilters(
139 const RenderPassDrawQuad
* quad
,
140 const gfx::Transform
& contents_device_transform
,
141 const gfx::Transform
& contents_device_transformInverse
);
142 void DrawRenderPassQuad(DrawingFrame
* frame
, const RenderPassDrawQuad
* quad
);
143 void DrawSolidColorQuad(const DrawingFrame
* frame
,
144 const SolidColorDrawQuad
* quad
);
145 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
146 const StreamVideoDrawQuad
* quad
);
147 void EnqueueTextureQuad(const DrawingFrame
* frame
,
148 const TextureDrawQuad
* quad
);
149 void FlushTextureQuadCache();
150 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
151 const IOSurfaceDrawQuad
* quad
);
152 void DrawTileQuad(const DrawingFrame
* frame
, const TileDrawQuad
* quad
);
153 void DrawContentQuad(const DrawingFrame
* frame
,
154 const ContentDrawQuadBase
* quad
,
155 ResourceProvider::ResourceId resource_id
);
156 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
157 const YUVVideoDrawQuad
* quad
);
158 void DrawPictureQuad(const DrawingFrame
* frame
,
159 const PictureDrawQuad
* quad
);
160 void DrawPictureQuadDirectToBackbuffer(const DrawingFrame
* frame
,
161 const PictureDrawQuad
* quad
);
163 void SetShaderOpacity(float opacity
, int alpha_location
);
164 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
165 void DrawQuadGeometry(const DrawingFrame
* frame
,
166 const gfx::Transform
& draw_transform
,
167 const gfx::RectF
& quad_rect
,
168 int matrix_location
);
169 void SetUseProgram(unsigned program
);
171 void CopyTextureToFramebuffer(const DrawingFrame
* frame
,
174 const gfx::Transform
& draw_matrix
,
175 bool flip_vertically
);
177 // Check if quad needs antialiasing and if so, inflate the quad and
178 // fill edge array for fragment shader. local_quad is set to
179 // inflated quad if antialiasing is required, otherwise it is left
180 // unchanged. edge array is filled with inflated quad's edge data
181 // if antialiasing is required, otherwise it is left unchanged.
182 // Returns true if quad requires antialiasing and false otherwise.
183 bool SetupQuadForAntialiasing(const gfx::Transform
& device_transform
,
184 const DrawQuad
* quad
,
185 gfx::QuadF
* local_quad
,
186 float edge
[24]) const;
188 bool UseScopedTexture(DrawingFrame
* frame
,
189 const ScopedResource
* resource
,
190 gfx::Rect viewport_rect
);
192 bool MakeContextCurrent();
194 bool InitializeSharedObjects();
195 void CleanupSharedObjects();
197 typedef base::Callback
<void(scoped_ptr
<CopyOutputRequest
> copy_request
,
199 AsyncGetFramebufferPixelsCleanupCallback
;
200 void DoGetFramebufferPixels(
202 gfx::Rect window_rect
,
203 const AsyncGetFramebufferPixelsCleanupCallback
& cleanup_callback
);
204 void FinishedReadback(
205 const AsyncGetFramebufferPixelsCleanupCallback
& cleanup_callback
,
206 unsigned source_buffer
,
208 uint8_t* dest_pixels
,
210 void PassOnSkBitmap(scoped_ptr
<SkBitmap
> bitmap
,
211 scoped_ptr
<SkAutoLockPixels
> lock
,
212 scoped_ptr
<CopyOutputRequest
> request
,
215 void ReinitializeGrCanvas();
216 void ReinitializeGLState();
218 virtual void DiscardBackbuffer() OVERRIDE
;
219 virtual void EnsureBackbuffer() OVERRIDE
;
220 void EnforceMemoryPolicy();
222 RendererCapabilities capabilities_
;
224 unsigned offscreen_framebuffer_id_
;
226 scoped_ptr
<GeometryBinding
> shared_geometry_
;
227 gfx::QuadF shared_geometry_quad_
;
229 // This block of bindings defines all of the programs used by the compositor
230 // itself. Add any new programs here to GLRendererShaderTest.
232 // Tiled layer shaders.
233 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
235 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
237 typedef ProgramBinding
<VertexShaderTileAA
,
238 FragmentShaderRGBATexClampSwizzleAlphaAA
>
239 TileProgramSwizzleAA
;
240 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
242 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
244 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
245 TileProgramSwizzleOpaque
;
246 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
247 TileCheckerboardProgram
;
250 typedef ProgramBinding
<VertexShaderPosTexTransform
,
251 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
252 typedef ProgramBinding
<VertexShaderPosTexTransform
,
253 FragmentShaderRGBATexPremultiplyAlpha
>
254 NonPremultipliedTextureProgram
;
255 typedef ProgramBinding
<VertexShaderPosTexTransform
,
256 FragmentShaderTexBackgroundVaryingAlpha
>
257 TextureBackgroundProgram
;
258 typedef ProgramBinding
<VertexShaderPosTexTransform
,
259 FragmentShaderTexBackgroundPremultiplyAlpha
>
260 NonPremultipliedTextureBackgroundProgram
;
261 typedef ProgramBinding
<VertexShaderPosTexTransform
,
262 FragmentShaderRGBATexRectVaryingAlpha
>
263 TextureIOSurfaceProgram
;
265 // Render surface shaders.
266 typedef ProgramBinding
<VertexShaderPosTexTransform
,
267 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
268 typedef ProgramBinding
<VertexShaderPosTexTransform
,
269 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
270 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
271 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
272 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
273 FragmentShaderRGBATexAlphaMaskAA
>
274 RenderPassMaskProgramAA
;
275 typedef ProgramBinding
<VertexShaderPosTexTransform
,
276 FragmentShaderRGBATexColorMatrixAlpha
>
277 RenderPassColorMatrixProgram
;
278 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
279 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
280 RenderPassMaskColorMatrixProgramAA
;
281 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
282 FragmentShaderRGBATexAlphaColorMatrixAA
>
283 RenderPassColorMatrixProgramAA
;
284 typedef ProgramBinding
<VertexShaderPosTexTransform
,
285 FragmentShaderRGBATexAlphaMaskColorMatrix
>
286 RenderPassMaskColorMatrixProgram
;
289 typedef ProgramBinding
<VertexShaderVideoTransform
,
290 FragmentShaderOESImageExternal
>
291 VideoStreamTextureProgram
;
292 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVVideo
>
294 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVAVideo
>
297 // Special purpose / effects shaders.
298 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
300 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
302 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
305 const TileProgram
* GetTileProgram(TexCoordPrecision precision
);
306 const TileProgramOpaque
* GetTileProgramOpaque(TexCoordPrecision precision
);
307 const TileProgramAA
* GetTileProgramAA(TexCoordPrecision precision
);
308 const TileProgramSwizzle
* GetTileProgramSwizzle(TexCoordPrecision precision
);
309 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
310 TexCoordPrecision precision
);
311 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
312 TexCoordPrecision precision
);
313 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
315 const RenderPassProgram
* GetRenderPassProgram(
316 TexCoordPrecision precision
);
317 const RenderPassProgramAA
* GetRenderPassProgramAA(
318 TexCoordPrecision precision
);
319 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
320 TexCoordPrecision precision
);
321 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
322 TexCoordPrecision precision
);
323 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
324 TexCoordPrecision precision
);
325 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
326 TexCoordPrecision precision
);
327 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
328 TexCoordPrecision precision
);
329 const RenderPassMaskColorMatrixProgramAA
*
330 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
);
332 const TextureProgram
* GetTextureProgram(
333 TexCoordPrecision precision
);
334 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
335 TexCoordPrecision precision
);
336 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
337 TexCoordPrecision precision
);
338 const NonPremultipliedTextureBackgroundProgram
*
339 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
340 const TextureIOSurfaceProgram
* GetTextureIOSurfaceProgram(
341 TexCoordPrecision precision
);
343 const VideoYUVProgram
* GetVideoYUVProgram(
344 TexCoordPrecision precision
);
345 const VideoYUVAProgram
* GetVideoYUVAProgram(
346 TexCoordPrecision precision
);
347 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
348 TexCoordPrecision precision
);
350 const DebugBorderProgram
* GetDebugBorderProgram();
351 const SolidColorProgram
* GetSolidColorProgram();
352 const SolidColorProgramAA
* GetSolidColorProgramAA();
354 scoped_ptr
<TileProgram
> tile_program_
;
355 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_
;
356 scoped_ptr
<TileProgramAA
> tile_program_aa_
;
357 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_
;
358 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_
;
359 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_
;
360 scoped_ptr
<TileCheckerboardProgram
> tile_checkerboard_program_
;
362 scoped_ptr
<TileProgram
> tile_program_highp_
;
363 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_highp_
;
364 scoped_ptr
<TileProgramAA
> tile_program_aa_highp_
;
365 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_highp_
;
366 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_highp_
;
367 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_highp_
;
369 scoped_ptr
<TextureProgram
> texture_program_
;
370 scoped_ptr
<NonPremultipliedTextureProgram
> nonpremultiplied_texture_program_
;
371 scoped_ptr
<TextureBackgroundProgram
> texture_background_program_
;
372 scoped_ptr
<NonPremultipliedTextureBackgroundProgram
>
373 nonpremultiplied_texture_background_program_
;
374 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_
;
376 scoped_ptr
<TextureProgram
> texture_program_highp_
;
377 scoped_ptr
<NonPremultipliedTextureProgram
>
378 nonpremultiplied_texture_program_highp_
;
379 scoped_ptr
<TextureBackgroundProgram
> texture_background_program_highp_
;
380 scoped_ptr
<NonPremultipliedTextureBackgroundProgram
>
381 nonpremultiplied_texture_background_program_highp_
;
382 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_highp_
;
384 scoped_ptr
<RenderPassProgram
> render_pass_program_
;
385 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_
;
386 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_
;
387 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_
;
388 scoped_ptr
<RenderPassColorMatrixProgram
> render_pass_color_matrix_program_
;
389 scoped_ptr
<RenderPassColorMatrixProgramAA
>
390 render_pass_color_matrix_program_aa_
;
391 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
392 render_pass_mask_color_matrix_program_
;
393 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
394 render_pass_mask_color_matrix_program_aa_
;
396 scoped_ptr
<RenderPassProgram
> render_pass_program_highp_
;
397 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_highp_
;
398 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_highp_
;
399 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_highp_
;
400 scoped_ptr
<RenderPassColorMatrixProgram
>
401 render_pass_color_matrix_program_highp_
;
402 scoped_ptr
<RenderPassColorMatrixProgramAA
>
403 render_pass_color_matrix_program_aa_highp_
;
404 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
405 render_pass_mask_color_matrix_program_highp_
;
406 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
407 render_pass_mask_color_matrix_program_aa_highp_
;
409 scoped_ptr
<VideoYUVProgram
> video_yuv_program_
;
410 scoped_ptr
<VideoYUVAProgram
> video_yuva_program_
;
411 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_
;
413 scoped_ptr
<VideoYUVProgram
> video_yuv_program_highp_
;
414 scoped_ptr
<VideoYUVAProgram
> video_yuva_program_highp_
;
415 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_highp_
;
417 scoped_ptr
<DebugBorderProgram
> debug_border_program_
;
418 scoped_ptr
<SolidColorProgram
> solid_color_program_
;
419 scoped_ptr
<SolidColorProgramAA
> solid_color_program_aa_
;
421 WebKit::WebGraphicsContext3D
* context_
;
423 skia::RefPtr
<GrContext
> gr_context_
;
424 skia::RefPtr
<SkCanvas
> sk_canvas_
;
426 gfx::Rect swap_buffer_rect_
;
427 gfx::Rect scissor_rect_
;
429 bool is_backbuffer_discarded_
;
430 bool discard_backbuffer_when_not_visible_
;
431 bool is_using_bind_uniform_
;
433 bool is_scissor_enabled_
;
434 bool stencil_shadow_
;
436 unsigned program_shadow_
;
437 TexturedQuadDrawCache draw_cache_
;
438 int highp_threshold_min_
;
439 int highp_threshold_cache_
;
440 bool offscreen_context_labelled_
;
442 struct PendingAsyncReadPixels
;
443 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
445 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
447 scoped_refptr
<ResourceProvider::Fence
> last_swap_fence_
;
449 SkBitmap on_demand_tile_raster_bitmap_
;
450 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
452 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
455 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
456 // call made by the compositor. Useful for debugging rendering issues but
457 // will significantly degrade performance.
458 #define DEBUG_GL_CALLS 0
460 #if DEBUG_GL_CALLS && !defined(NDEBUG)
461 #define GLC(context, x) \
462 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
464 #define GLC(context, x) (x)
469 #endif // CC_OUTPUT_GL_RENDERER_H_