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();
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 static void DeleteTextureReleaseCallback(
216 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
217 base::WeakPtr
<GLRenderer
> gl_renderer
,
221 void DeleteTextureReleaseCallbackOnImplThread(unsigned texture_id
,
225 void ReinitializeGrCanvas();
226 void ReinitializeGLState();
228 virtual void DiscardBackbuffer() OVERRIDE
;
229 virtual void EnsureBackbuffer() OVERRIDE
;
230 void EnforceMemoryPolicy();
232 RendererCapabilities capabilities_
;
234 unsigned offscreen_framebuffer_id_
;
236 scoped_ptr
<GeometryBinding
> shared_geometry_
;
237 gfx::QuadF shared_geometry_quad_
;
239 // This block of bindings defines all of the programs used by the compositor
240 // itself. Add any new programs here to GLRendererShaderTest.
242 // Tiled layer shaders.
243 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
245 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
247 typedef ProgramBinding
<VertexShaderTileAA
,
248 FragmentShaderRGBATexClampSwizzleAlphaAA
>
249 TileProgramSwizzleAA
;
250 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
252 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
254 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
255 TileProgramSwizzleOpaque
;
256 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
257 TileCheckerboardProgram
;
260 typedef ProgramBinding
<VertexShaderPosTexTransform
,
261 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
262 typedef ProgramBinding
<VertexShaderPosTexTransform
,
263 FragmentShaderRGBATexPremultiplyAlpha
>
264 NonPremultipliedTextureProgram
;
265 typedef ProgramBinding
<VertexShaderPosTexTransform
,
266 FragmentShaderTexBackgroundVaryingAlpha
>
267 TextureBackgroundProgram
;
268 typedef ProgramBinding
<VertexShaderPosTexTransform
,
269 FragmentShaderTexBackgroundPremultiplyAlpha
>
270 NonPremultipliedTextureBackgroundProgram
;
271 typedef ProgramBinding
<VertexShaderPosTexTransform
,
272 FragmentShaderRGBATexRectVaryingAlpha
>
273 TextureIOSurfaceProgram
;
275 // Render surface shaders.
276 typedef ProgramBinding
<VertexShaderPosTexTransform
,
277 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
278 typedef ProgramBinding
<VertexShaderPosTexTransform
,
279 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
280 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
281 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
282 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
283 FragmentShaderRGBATexAlphaMaskAA
>
284 RenderPassMaskProgramAA
;
285 typedef ProgramBinding
<VertexShaderPosTexTransform
,
286 FragmentShaderRGBATexColorMatrixAlpha
>
287 RenderPassColorMatrixProgram
;
288 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
289 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
290 RenderPassMaskColorMatrixProgramAA
;
291 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
292 FragmentShaderRGBATexAlphaColorMatrixAA
>
293 RenderPassColorMatrixProgramAA
;
294 typedef ProgramBinding
<VertexShaderPosTexTransform
,
295 FragmentShaderRGBATexAlphaMaskColorMatrix
>
296 RenderPassMaskColorMatrixProgram
;
299 typedef ProgramBinding
<VertexShaderVideoTransform
,
300 FragmentShaderOESImageExternal
>
301 VideoStreamTextureProgram
;
302 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVVideo
>
304 typedef ProgramBinding
<VertexShaderPosTexYUVStretch
, FragmentShaderYUVAVideo
>
307 // Special purpose / effects shaders.
308 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
310 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
312 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
315 const TileProgram
* GetTileProgram(TexCoordPrecision precision
);
316 const TileProgramOpaque
* GetTileProgramOpaque(TexCoordPrecision precision
);
317 const TileProgramAA
* GetTileProgramAA(TexCoordPrecision precision
);
318 const TileProgramSwizzle
* GetTileProgramSwizzle(TexCoordPrecision precision
);
319 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
320 TexCoordPrecision precision
);
321 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
322 TexCoordPrecision precision
);
323 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
325 const RenderPassProgram
* GetRenderPassProgram(
326 TexCoordPrecision precision
);
327 const RenderPassProgramAA
* GetRenderPassProgramAA(
328 TexCoordPrecision precision
);
329 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
330 TexCoordPrecision precision
);
331 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
332 TexCoordPrecision precision
);
333 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
334 TexCoordPrecision precision
);
335 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
336 TexCoordPrecision precision
);
337 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
338 TexCoordPrecision precision
);
339 const RenderPassMaskColorMatrixProgramAA
*
340 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
);
342 const TextureProgram
* GetTextureProgram(
343 TexCoordPrecision precision
);
344 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
345 TexCoordPrecision precision
);
346 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
347 TexCoordPrecision precision
);
348 const NonPremultipliedTextureBackgroundProgram
*
349 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
350 const TextureIOSurfaceProgram
* GetTextureIOSurfaceProgram(
351 TexCoordPrecision precision
);
353 const VideoYUVProgram
* GetVideoYUVProgram(
354 TexCoordPrecision precision
);
355 const VideoYUVAProgram
* GetVideoYUVAProgram(
356 TexCoordPrecision precision
);
357 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
358 TexCoordPrecision precision
);
360 const DebugBorderProgram
* GetDebugBorderProgram();
361 const SolidColorProgram
* GetSolidColorProgram();
362 const SolidColorProgramAA
* GetSolidColorProgramAA();
364 scoped_ptr
<TileProgram
> tile_program_
;
365 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_
;
366 scoped_ptr
<TileProgramAA
> tile_program_aa_
;
367 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_
;
368 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_
;
369 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_
;
370 scoped_ptr
<TileCheckerboardProgram
> tile_checkerboard_program_
;
372 scoped_ptr
<TileProgram
> tile_program_highp_
;
373 scoped_ptr
<TileProgramOpaque
> tile_program_opaque_highp_
;
374 scoped_ptr
<TileProgramAA
> tile_program_aa_highp_
;
375 scoped_ptr
<TileProgramSwizzle
> tile_program_swizzle_highp_
;
376 scoped_ptr
<TileProgramSwizzleOpaque
> tile_program_swizzle_opaque_highp_
;
377 scoped_ptr
<TileProgramSwizzleAA
> tile_program_swizzle_aa_highp_
;
379 scoped_ptr
<TextureProgram
> texture_program_
;
380 scoped_ptr
<NonPremultipliedTextureProgram
> nonpremultiplied_texture_program_
;
381 scoped_ptr
<TextureBackgroundProgram
> texture_background_program_
;
382 scoped_ptr
<NonPremultipliedTextureBackgroundProgram
>
383 nonpremultiplied_texture_background_program_
;
384 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_
;
386 scoped_ptr
<TextureProgram
> texture_program_highp_
;
387 scoped_ptr
<NonPremultipliedTextureProgram
>
388 nonpremultiplied_texture_program_highp_
;
389 scoped_ptr
<TextureBackgroundProgram
> texture_background_program_highp_
;
390 scoped_ptr
<NonPremultipliedTextureBackgroundProgram
>
391 nonpremultiplied_texture_background_program_highp_
;
392 scoped_ptr
<TextureIOSurfaceProgram
> texture_io_surface_program_highp_
;
394 scoped_ptr
<RenderPassProgram
> render_pass_program_
;
395 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_
;
396 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_
;
397 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_
;
398 scoped_ptr
<RenderPassColorMatrixProgram
> render_pass_color_matrix_program_
;
399 scoped_ptr
<RenderPassColorMatrixProgramAA
>
400 render_pass_color_matrix_program_aa_
;
401 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
402 render_pass_mask_color_matrix_program_
;
403 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
404 render_pass_mask_color_matrix_program_aa_
;
406 scoped_ptr
<RenderPassProgram
> render_pass_program_highp_
;
407 scoped_ptr
<RenderPassProgramAA
> render_pass_program_aa_highp_
;
408 scoped_ptr
<RenderPassMaskProgram
> render_pass_mask_program_highp_
;
409 scoped_ptr
<RenderPassMaskProgramAA
> render_pass_mask_program_aa_highp_
;
410 scoped_ptr
<RenderPassColorMatrixProgram
>
411 render_pass_color_matrix_program_highp_
;
412 scoped_ptr
<RenderPassColorMatrixProgramAA
>
413 render_pass_color_matrix_program_aa_highp_
;
414 scoped_ptr
<RenderPassMaskColorMatrixProgram
>
415 render_pass_mask_color_matrix_program_highp_
;
416 scoped_ptr
<RenderPassMaskColorMatrixProgramAA
>
417 render_pass_mask_color_matrix_program_aa_highp_
;
419 scoped_ptr
<VideoYUVProgram
> video_yuv_program_
;
420 scoped_ptr
<VideoYUVAProgram
> video_yuva_program_
;
421 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_
;
423 scoped_ptr
<VideoYUVProgram
> video_yuv_program_highp_
;
424 scoped_ptr
<VideoYUVAProgram
> video_yuva_program_highp_
;
425 scoped_ptr
<VideoStreamTextureProgram
> video_stream_texture_program_highp_
;
427 scoped_ptr
<DebugBorderProgram
> debug_border_program_
;
428 scoped_ptr
<SolidColorProgram
> solid_color_program_
;
429 scoped_ptr
<SolidColorProgramAA
> solid_color_program_aa_
;
431 WebKit::WebGraphicsContext3D
* context_
;
433 skia::RefPtr
<GrContext
> gr_context_
;
434 skia::RefPtr
<SkCanvas
> sk_canvas_
;
436 gfx::Rect swap_buffer_rect_
;
437 gfx::Rect scissor_rect_
;
439 bool is_backbuffer_discarded_
;
440 bool discard_backbuffer_when_not_visible_
;
441 bool is_using_bind_uniform_
;
443 bool is_scissor_enabled_
;
444 bool stencil_shadow_
;
446 unsigned program_shadow_
;
447 TexturedQuadDrawCache draw_cache_
;
448 int highp_threshold_min_
;
449 int highp_threshold_cache_
;
450 bool offscreen_context_labelled_
;
452 struct PendingAsyncReadPixels
;
453 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
455 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
457 scoped_refptr
<ResourceProvider::Fence
> last_swap_fence_
;
459 SkBitmap on_demand_tile_raster_bitmap_
;
460 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
462 base::WeakPtrFactory
<GLRenderer
> weak_factory_
;
464 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
467 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
468 // call made by the compositor. Useful for debugging rendering issues but
469 // will significantly degrade performance.
470 #define DEBUG_GL_CALLS 0
472 #if DEBUG_GL_CALLS && !defined(NDEBUG)
473 #define GLC(context, x) \
474 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
476 #define GLC(context, x) (x)
481 #endif // CC_OUTPUT_GL_RENDERER_H_