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_deque.h"
11 #include "cc/base/scoped_ptr_vector.h"
12 #include "cc/output/direct_renderer.h"
13 #include "cc/output/gl_renderer_draw_cache.h"
14 #include "cc/output/program_binding.h"
15 #include "cc/output/renderer.h"
16 #include "cc/quads/checkerboard_draw_quad.h"
17 #include "cc/quads/debug_border_draw_quad.h"
18 #include "cc/quads/io_surface_draw_quad.h"
19 #include "cc/quads/render_pass_draw_quad.h"
20 #include "cc/quads/solid_color_draw_quad.h"
21 #include "cc/quads/tile_draw_quad.h"
22 #include "cc/quads/yuv_video_draw_quad.h"
23 #include "ui/gfx/quad_f.h"
35 class GLRendererShaderTest
;
37 class PictureDrawQuad
;
39 class StreamVideoDrawQuad
;
40 class TextureDrawQuad
;
41 class TextureMailboxDeleter
;
42 class GeometryBinding
;
43 class ScopedEnsureFramebufferAllocation
;
45 // Class that handles drawing of composited render layers using GL.
46 class CC_EXPORT GLRenderer
: public DirectRenderer
{
48 class ScopedUseGrContext
;
50 static scoped_ptr
<GLRenderer
> Create(
51 RendererClient
* client
,
52 const LayerTreeSettings
* settings
,
53 OutputSurface
* output_surface
,
54 ResourceProvider
* resource_provider
,
55 TextureMailboxDeleter
* texture_mailbox_deleter
,
56 int highp_threshold_min
);
58 virtual ~GLRenderer();
60 virtual const RendererCapabilitiesImpl
& Capabilities() const OVERRIDE
;
62 // Waits for rendering to finish.
63 virtual void Finish() OVERRIDE
;
65 virtual void DoNoOp() OVERRIDE
;
66 virtual void SwapBuffers(const CompositorFrameMetadata
& metadata
) OVERRIDE
;
68 virtual bool IsContextLost() OVERRIDE
;
70 virtual void SendManagedMemoryStats(size_t bytes_visible
,
71 size_t bytes_visible_and_nearby
,
72 size_t bytes_allocated
) OVERRIDE
;
74 static void DebugGLCall(gpu::gles2::GLES2Interface
* gl
,
80 GLRenderer(RendererClient
* client
,
81 const LayerTreeSettings
* settings
,
82 OutputSurface
* output_surface
,
83 ResourceProvider
* resource_provider
,
84 TextureMailboxDeleter
* texture_mailbox_deleter
,
85 int highp_threshold_min
);
87 virtual void DidChangeVisibility() OVERRIDE
;
89 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_
; }
91 const gfx::QuadF
& SharedGeometryQuad() const { return shared_geometry_quad_
; }
92 const GeometryBinding
* SharedGeometry() const {
93 return shared_geometry_
.get();
96 void GetFramebufferPixelsAsync(const gfx::Rect
& rect
,
97 scoped_ptr
<CopyOutputRequest
> request
);
98 void GetFramebufferTexture(unsigned texture_id
,
99 ResourceFormat texture_format
,
100 const gfx::Rect
& device_rect
);
101 void ReleaseRenderPassTextures();
103 void SetStencilEnabled(bool enabled
);
104 bool stencil_enabled() const { return stencil_shadow_
; }
105 void SetBlendEnabled(bool enabled
);
106 bool blend_enabled() const { return blend_shadow_
; }
108 virtual void BindFramebufferToOutputSurface(DrawingFrame
* frame
) OVERRIDE
;
109 virtual bool BindFramebufferToTexture(DrawingFrame
* frame
,
110 const ScopedResource
* resource
,
111 const gfx::Rect
& target_rect
) OVERRIDE
;
112 virtual void SetDrawViewport(const gfx::Rect
& window_space_viewport
) OVERRIDE
;
113 virtual void SetScissorTestRect(const gfx::Rect
& scissor_rect
) OVERRIDE
;
114 virtual void DiscardPixels(bool has_external_stencil_test
,
115 bool draw_rect_covers_full_surface
) OVERRIDE
;
116 virtual void ClearFramebuffer(DrawingFrame
* frame
,
117 bool has_external_stencil_test
) OVERRIDE
;
118 virtual void DoDrawQuad(DrawingFrame
* frame
, const class DrawQuad
*) OVERRIDE
;
119 virtual void BeginDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
120 virtual void FinishDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
121 virtual bool FlippedFramebuffer() const OVERRIDE
;
122 virtual void EnsureScissorTestEnabled() OVERRIDE
;
123 virtual void EnsureScissorTestDisabled() OVERRIDE
;
124 virtual void CopyCurrentRenderPassToBitmap(
126 scoped_ptr
<CopyOutputRequest
> request
) OVERRIDE
;
127 virtual void FinishDrawingQuadList() OVERRIDE
;
129 // Check if quad needs antialiasing and if so, inflate the quad and
130 // fill edge array for fragment shader. local_quad is set to
131 // inflated quad if antialiasing is required, otherwise it is left
132 // unchanged. edge array is filled with inflated quad's edge data
133 // if antialiasing is required, otherwise it is left unchanged.
134 // Returns true if quad requires antialiasing and false otherwise.
135 static bool SetupQuadForAntialiasing(const gfx::Transform
& device_transform
,
136 const DrawQuad
* quad
,
137 gfx::QuadF
* local_quad
,
141 friend class GLRendererShaderPixelTest
;
142 friend class GLRendererShaderTest
;
144 static void ToGLMatrix(float* gl_matrix
, const gfx::Transform
& transform
);
146 void DrawCheckerboardQuad(const DrawingFrame
* frame
,
147 const CheckerboardDrawQuad
* quad
);
148 void DrawDebugBorderQuad(const DrawingFrame
* frame
,
149 const DebugBorderDrawQuad
* quad
);
150 scoped_ptr
<ScopedResource
> GetBackgroundWithFilters(
152 const RenderPassDrawQuad
* quad
,
153 const gfx::Transform
& contents_device_transform
,
154 const gfx::Transform
& contents_device_transformInverse
,
155 bool* background_changed
);
156 void DrawRenderPassQuad(DrawingFrame
* frame
, const RenderPassDrawQuad
* quad
);
157 void DrawSolidColorQuad(const DrawingFrame
* frame
,
158 const SolidColorDrawQuad
* quad
);
159 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
160 const StreamVideoDrawQuad
* quad
);
161 void EnqueueTextureQuad(const DrawingFrame
* frame
,
162 const TextureDrawQuad
* quad
);
163 void FlushTextureQuadCache();
164 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
165 const IOSurfaceDrawQuad
* quad
);
166 void DrawTileQuad(const DrawingFrame
* frame
, const TileDrawQuad
* quad
);
167 void DrawContentQuad(const DrawingFrame
* frame
,
168 const ContentDrawQuadBase
* quad
,
169 ResourceProvider::ResourceId resource_id
);
170 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
171 const YUVVideoDrawQuad
* quad
);
172 void DrawPictureQuad(const DrawingFrame
* frame
,
173 const PictureDrawQuad
* quad
);
175 void SetShaderOpacity(float opacity
, int alpha_location
);
176 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
177 void DrawQuadGeometry(const DrawingFrame
* frame
,
178 const gfx::Transform
& draw_transform
,
179 const gfx::RectF
& quad_rect
,
180 int matrix_location
);
181 void SetUseProgram(unsigned program
);
183 void CopyTextureToFramebuffer(const DrawingFrame
* frame
,
185 const gfx::Rect
& rect
,
186 const gfx::Transform
& draw_matrix
,
187 bool flip_vertically
);
189 bool UseScopedTexture(DrawingFrame
* frame
,
190 const ScopedResource
* resource
,
191 const gfx::Rect
& viewport_rect
);
193 bool MakeContextCurrent();
195 void InitializeSharedObjects();
196 void CleanupSharedObjects();
198 typedef base::Callback
<void(scoped_ptr
<CopyOutputRequest
> copy_request
,
200 AsyncGetFramebufferPixelsCleanupCallback
;
201 void FinishedReadback(unsigned source_buffer
,
203 const gfx::Size
& size
);
205 void ReinitializeGLState();
206 void RestoreGLState();
207 void RestoreFramebuffer(DrawingFrame
* frame
);
209 virtual void DiscardBackbuffer() OVERRIDE
;
210 virtual void EnsureBackbuffer() OVERRIDE
;
211 void EnforceMemoryPolicy();
213 void ScheduleOverlays(DrawingFrame
* frame
);
215 typedef ScopedPtrVector
<ResourceProvider::ScopedReadLockGL
>
216 OverlayResourceLockList
;
217 OverlayResourceLockList pending_overlay_resources_
;
218 OverlayResourceLockList in_use_overlay_resources_
;
220 RendererCapabilitiesImpl capabilities_
;
222 unsigned offscreen_framebuffer_id_
;
224 scoped_ptr
<GeometryBinding
> shared_geometry_
;
225 gfx::QuadF shared_geometry_quad_
;
227 // This block of bindings defines all of the programs used by the compositor
228 // itself. Add any new programs here to GLRendererShaderTest.
230 // Tiled layer shaders.
231 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
233 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
235 typedef ProgramBinding
<VertexShaderTileAA
,
236 FragmentShaderRGBATexClampSwizzleAlphaAA
>
237 TileProgramSwizzleAA
;
238 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
240 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
242 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
243 TileProgramSwizzleOpaque
;
244 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
245 TileCheckerboardProgram
;
248 typedef ProgramBinding
<VertexShaderPosTexTransform
,
249 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
250 typedef ProgramBinding
<VertexShaderPosTexTransform
,
251 FragmentShaderRGBATexPremultiplyAlpha
>
252 NonPremultipliedTextureProgram
;
253 typedef ProgramBinding
<VertexShaderPosTexTransform
,
254 FragmentShaderTexBackgroundVaryingAlpha
>
255 TextureBackgroundProgram
;
256 typedef ProgramBinding
<VertexShaderPosTexTransform
,
257 FragmentShaderTexBackgroundPremultiplyAlpha
>
258 NonPremultipliedTextureBackgroundProgram
;
260 // Render surface shaders.
261 typedef ProgramBinding
<VertexShaderPosTexTransform
,
262 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
263 typedef ProgramBinding
<VertexShaderPosTexTransform
,
264 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
265 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
266 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
267 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
268 FragmentShaderRGBATexAlphaMaskAA
>
269 RenderPassMaskProgramAA
;
270 typedef ProgramBinding
<VertexShaderPosTexTransform
,
271 FragmentShaderRGBATexColorMatrixAlpha
>
272 RenderPassColorMatrixProgram
;
273 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
274 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
275 RenderPassMaskColorMatrixProgramAA
;
276 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
277 FragmentShaderRGBATexAlphaColorMatrixAA
>
278 RenderPassColorMatrixProgramAA
;
279 typedef ProgramBinding
<VertexShaderPosTexTransform
,
280 FragmentShaderRGBATexAlphaMaskColorMatrix
>
281 RenderPassMaskColorMatrixProgram
;
284 typedef ProgramBinding
<VertexShaderVideoTransform
, FragmentShaderRGBATex
>
285 VideoStreamTextureProgram
;
286 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
287 FragmentShaderYUVVideo
> VideoYUVProgram
;
288 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
289 FragmentShaderYUVAVideo
> VideoYUVAProgram
;
291 // Special purpose / effects shaders.
292 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
294 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
296 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
299 const TileProgram
* GetTileProgram(
300 TexCoordPrecision precision
, SamplerType sampler
);
301 const TileProgramOpaque
* GetTileProgramOpaque(
302 TexCoordPrecision precision
, SamplerType sampler
);
303 const TileProgramAA
* GetTileProgramAA(
304 TexCoordPrecision precision
, SamplerType sampler
);
305 const TileProgramSwizzle
* GetTileProgramSwizzle(
306 TexCoordPrecision precision
, SamplerType sampler
);
307 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
308 TexCoordPrecision precision
, SamplerType sampler
);
309 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
310 TexCoordPrecision precision
, SamplerType sampler
);
312 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
314 const RenderPassProgram
* GetRenderPassProgram(
315 TexCoordPrecision precision
);
316 const RenderPassProgramAA
* GetRenderPassProgramAA(
317 TexCoordPrecision precision
);
318 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
319 TexCoordPrecision precision
);
320 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
321 TexCoordPrecision precision
);
322 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
323 TexCoordPrecision precision
);
324 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
325 TexCoordPrecision precision
);
326 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
327 TexCoordPrecision precision
);
328 const RenderPassMaskColorMatrixProgramAA
*
329 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
);
331 const TextureProgram
* GetTextureProgram(
332 TexCoordPrecision precision
);
333 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
334 TexCoordPrecision precision
);
335 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
336 TexCoordPrecision precision
);
337 const NonPremultipliedTextureBackgroundProgram
*
338 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
339 const TextureProgram
* GetTextureIOSurfaceProgram(
340 TexCoordPrecision precision
);
342 const VideoYUVProgram
* GetVideoYUVProgram(
343 TexCoordPrecision precision
);
344 const VideoYUVAProgram
* GetVideoYUVAProgram(
345 TexCoordPrecision precision
);
346 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
347 TexCoordPrecision precision
);
349 const DebugBorderProgram
* GetDebugBorderProgram();
350 const SolidColorProgram
* GetSolidColorProgram();
351 const SolidColorProgramAA
* GetSolidColorProgramAA();
353 TileProgram tile_program_
[NumTexCoordPrecisions
][NumSamplerTypes
];
355 tile_program_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
356 TileProgramAA tile_program_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
358 tile_program_swizzle_
[NumTexCoordPrecisions
][NumSamplerTypes
];
359 TileProgramSwizzleOpaque
360 tile_program_swizzle_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
362 tile_program_swizzle_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
364 TileCheckerboardProgram tile_checkerboard_program_
;
366 TextureProgram texture_program_
[NumTexCoordPrecisions
];
367 NonPremultipliedTextureProgram
368 nonpremultiplied_texture_program_
[NumTexCoordPrecisions
];
369 TextureBackgroundProgram texture_background_program_
[NumTexCoordPrecisions
];
370 NonPremultipliedTextureBackgroundProgram
371 nonpremultiplied_texture_background_program_
[NumTexCoordPrecisions
];
372 TextureProgram texture_io_surface_program_
[NumTexCoordPrecisions
];
374 RenderPassProgram render_pass_program_
[NumTexCoordPrecisions
];
375 RenderPassProgramAA render_pass_program_aa_
[NumTexCoordPrecisions
];
376 RenderPassMaskProgram render_pass_mask_program_
[NumTexCoordPrecisions
];
377 RenderPassMaskProgramAA render_pass_mask_program_aa_
[NumTexCoordPrecisions
];
378 RenderPassColorMatrixProgram
379 render_pass_color_matrix_program_
[NumTexCoordPrecisions
];
380 RenderPassColorMatrixProgramAA
381 render_pass_color_matrix_program_aa_
[NumTexCoordPrecisions
];
382 RenderPassMaskColorMatrixProgram
383 render_pass_mask_color_matrix_program_
[NumTexCoordPrecisions
];
384 RenderPassMaskColorMatrixProgramAA
385 render_pass_mask_color_matrix_program_aa_
[NumTexCoordPrecisions
];
387 VideoYUVProgram video_yuv_program_
[NumTexCoordPrecisions
];
388 VideoYUVAProgram video_yuva_program_
[NumTexCoordPrecisions
];
389 VideoStreamTextureProgram
390 video_stream_texture_program_
[NumTexCoordPrecisions
];
392 DebugBorderProgram debug_border_program_
;
393 SolidColorProgram solid_color_program_
;
394 SolidColorProgramAA solid_color_program_aa_
;
396 gpu::gles2::GLES2Interface
* gl_
;
397 gpu::ContextSupport
* context_support_
;
399 skia::RefPtr
<GrContext
> gr_context_
;
400 skia::RefPtr
<SkCanvas
> sk_canvas_
;
402 TextureMailboxDeleter
* texture_mailbox_deleter_
;
404 gfx::Rect swap_buffer_rect_
;
405 gfx::Rect scissor_rect_
;
407 bool is_backbuffer_discarded_
;
408 bool is_using_bind_uniform_
;
409 bool is_scissor_enabled_
;
410 bool scissor_rect_needs_reset_
;
411 bool stencil_shadow_
;
413 unsigned program_shadow_
;
414 TexturedQuadDrawCache draw_cache_
;
415 int highp_threshold_min_
;
416 int highp_threshold_cache_
;
418 struct PendingAsyncReadPixels
;
419 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
421 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
424 ScopedPtrDeque
<SyncQuery
> pending_sync_queries_
;
425 ScopedPtrDeque
<SyncQuery
> available_sync_queries_
;
426 scoped_ptr
<SyncQuery
> current_sync_query_
;
427 bool use_sync_query_
;
429 SkBitmap on_demand_tile_raster_bitmap_
;
430 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
432 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
435 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
436 // call made by the compositor. Useful for debugging rendering issues but
437 // will significantly degrade performance.
438 #define DEBUG_GL_CALLS 0
440 #if DEBUG_GL_CALLS && !defined(NDEBUG)
441 #define GLC(context, x) \
442 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
444 #define GLC(context, x) (x)
449 #endif // CC_OUTPUT_GL_RENDERER_H_