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/geometry/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();
70 static void DebugGLCall(gpu::gles2::GLES2Interface
* gl
,
76 GLRenderer(RendererClient
* client
,
77 const LayerTreeSettings
* settings
,
78 OutputSurface
* output_surface
,
79 ResourceProvider
* resource_provider
,
80 TextureMailboxDeleter
* texture_mailbox_deleter
,
81 int highp_threshold_min
);
83 virtual void DidChangeVisibility() OVERRIDE
;
85 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_
; }
87 const gfx::QuadF
& SharedGeometryQuad() const { return shared_geometry_quad_
; }
88 const GeometryBinding
* SharedGeometry() const {
89 return shared_geometry_
.get();
92 void GetFramebufferPixelsAsync(const gfx::Rect
& rect
,
93 scoped_ptr
<CopyOutputRequest
> request
);
94 void GetFramebufferTexture(unsigned texture_id
,
95 ResourceFormat texture_format
,
96 const gfx::Rect
& device_rect
);
97 void ReleaseRenderPassTextures();
99 void SetStencilEnabled(bool enabled
);
100 bool stencil_enabled() const { return stencil_shadow_
; }
101 void SetBlendEnabled(bool enabled
);
102 bool blend_enabled() const { return blend_shadow_
; }
104 virtual void BindFramebufferToOutputSurface(DrawingFrame
* frame
) OVERRIDE
;
105 virtual bool BindFramebufferToTexture(DrawingFrame
* frame
,
106 const ScopedResource
* resource
,
107 const gfx::Rect
& target_rect
) OVERRIDE
;
108 virtual void SetDrawViewport(const gfx::Rect
& window_space_viewport
) OVERRIDE
;
109 virtual void SetScissorTestRect(const gfx::Rect
& scissor_rect
) OVERRIDE
;
110 virtual void DiscardPixels(bool has_external_stencil_test
,
111 bool draw_rect_covers_full_surface
) OVERRIDE
;
112 virtual void ClearFramebuffer(DrawingFrame
* frame
,
113 bool has_external_stencil_test
) OVERRIDE
;
114 virtual void DoDrawQuad(DrawingFrame
* frame
, const class DrawQuad
*) OVERRIDE
;
115 virtual void BeginDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
116 virtual void FinishDrawingFrame(DrawingFrame
* frame
) OVERRIDE
;
117 virtual bool FlippedFramebuffer() const OVERRIDE
;
118 virtual void EnsureScissorTestEnabled() OVERRIDE
;
119 virtual void EnsureScissorTestDisabled() OVERRIDE
;
120 virtual void CopyCurrentRenderPassToBitmap(
122 scoped_ptr
<CopyOutputRequest
> request
) OVERRIDE
;
123 virtual void FinishDrawingQuadList() OVERRIDE
;
125 // Check if quad needs antialiasing and if so, inflate the quad and
126 // fill edge array for fragment shader. local_quad is set to
127 // inflated quad if antialiasing is required, otherwise it is left
128 // unchanged. edge array is filled with inflated quad's edge data
129 // if antialiasing is required, otherwise it is left unchanged.
130 // Returns true if quad requires antialiasing and false otherwise.
131 static bool SetupQuadForAntialiasing(const gfx::Transform
& device_transform
,
132 const DrawQuad
* quad
,
133 gfx::QuadF
* local_quad
,
137 friend class GLRendererShaderPixelTest
;
138 friend class GLRendererShaderTest
;
140 static void ToGLMatrix(float* gl_matrix
, const gfx::Transform
& transform
);
142 void DrawCheckerboardQuad(const DrawingFrame
* frame
,
143 const CheckerboardDrawQuad
* quad
);
144 void DrawDebugBorderQuad(const DrawingFrame
* frame
,
145 const DebugBorderDrawQuad
* quad
);
146 scoped_ptr
<ScopedResource
> GetBackgroundWithFilters(
148 const RenderPassDrawQuad
* quad
,
149 const gfx::Transform
& contents_device_transform
,
150 const gfx::Transform
& contents_device_transformInverse
,
151 bool* background_changed
);
152 void DrawRenderPassQuad(DrawingFrame
* frame
, const RenderPassDrawQuad
* quad
);
153 void DrawSolidColorQuad(const DrawingFrame
* frame
,
154 const SolidColorDrawQuad
* quad
);
155 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
156 const StreamVideoDrawQuad
* quad
);
157 void EnqueueTextureQuad(const DrawingFrame
* frame
,
158 const TextureDrawQuad
* quad
);
159 void FlushTextureQuadCache();
160 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
161 const IOSurfaceDrawQuad
* quad
);
162 void DrawTileQuad(const DrawingFrame
* frame
, const TileDrawQuad
* quad
);
163 void DrawContentQuad(const DrawingFrame
* frame
,
164 const ContentDrawQuadBase
* quad
,
165 ResourceProvider::ResourceId resource_id
);
166 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
167 const YUVVideoDrawQuad
* quad
);
168 void DrawPictureQuad(const DrawingFrame
* frame
,
169 const PictureDrawQuad
* quad
);
171 void SetShaderOpacity(float opacity
, int alpha_location
);
172 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
173 void DrawQuadGeometry(const DrawingFrame
* frame
,
174 const gfx::Transform
& draw_transform
,
175 const gfx::RectF
& quad_rect
,
176 int matrix_location
);
177 void SetUseProgram(unsigned program
);
179 void CopyTextureToFramebuffer(const DrawingFrame
* frame
,
181 const gfx::Rect
& rect
,
182 const gfx::Transform
& draw_matrix
,
183 bool flip_vertically
);
185 bool UseScopedTexture(DrawingFrame
* frame
,
186 const ScopedResource
* resource
,
187 const gfx::Rect
& viewport_rect
);
189 bool MakeContextCurrent();
191 void InitializeSharedObjects();
192 void CleanupSharedObjects();
194 typedef base::Callback
<void(scoped_ptr
<CopyOutputRequest
> copy_request
,
196 AsyncGetFramebufferPixelsCleanupCallback
;
197 void FinishedReadback(unsigned source_buffer
,
199 const gfx::Size
& size
);
201 void ReinitializeGLState();
202 void RestoreGLState();
203 void RestoreFramebuffer(DrawingFrame
* frame
);
205 virtual void DiscardBackbuffer() OVERRIDE
;
206 virtual void EnsureBackbuffer() OVERRIDE
;
207 void EnforceMemoryPolicy();
209 void ScheduleOverlays(DrawingFrame
* frame
);
211 typedef ScopedPtrVector
<ResourceProvider::ScopedReadLockGL
>
212 OverlayResourceLockList
;
213 OverlayResourceLockList pending_overlay_resources_
;
214 OverlayResourceLockList in_use_overlay_resources_
;
216 RendererCapabilitiesImpl capabilities_
;
218 unsigned offscreen_framebuffer_id_
;
220 scoped_ptr
<GeometryBinding
> shared_geometry_
;
221 gfx::QuadF shared_geometry_quad_
;
223 // This block of bindings defines all of the programs used by the compositor
224 // itself. Add any new programs here to GLRendererShaderTest.
226 // Tiled layer shaders.
227 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
229 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
231 typedef ProgramBinding
<VertexShaderTileAA
,
232 FragmentShaderRGBATexClampSwizzleAlphaAA
>
233 TileProgramSwizzleAA
;
234 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
236 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
238 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
239 TileProgramSwizzleOpaque
;
240 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
241 TileCheckerboardProgram
;
244 typedef ProgramBinding
<VertexShaderPosTexTransform
,
245 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
246 typedef ProgramBinding
<VertexShaderPosTexTransform
,
247 FragmentShaderRGBATexPremultiplyAlpha
>
248 NonPremultipliedTextureProgram
;
249 typedef ProgramBinding
<VertexShaderPosTexTransform
,
250 FragmentShaderTexBackgroundVaryingAlpha
>
251 TextureBackgroundProgram
;
252 typedef ProgramBinding
<VertexShaderPosTexTransform
,
253 FragmentShaderTexBackgroundPremultiplyAlpha
>
254 NonPremultipliedTextureBackgroundProgram
;
256 // Render surface shaders.
257 typedef ProgramBinding
<VertexShaderPosTexTransform
,
258 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
259 typedef ProgramBinding
<VertexShaderPosTexTransform
,
260 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
261 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
262 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
263 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
264 FragmentShaderRGBATexAlphaMaskAA
>
265 RenderPassMaskProgramAA
;
266 typedef ProgramBinding
<VertexShaderPosTexTransform
,
267 FragmentShaderRGBATexColorMatrixAlpha
>
268 RenderPassColorMatrixProgram
;
269 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
270 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
271 RenderPassMaskColorMatrixProgramAA
;
272 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
273 FragmentShaderRGBATexAlphaColorMatrixAA
>
274 RenderPassColorMatrixProgramAA
;
275 typedef ProgramBinding
<VertexShaderPosTexTransform
,
276 FragmentShaderRGBATexAlphaMaskColorMatrix
>
277 RenderPassMaskColorMatrixProgram
;
280 typedef ProgramBinding
<VertexShaderVideoTransform
, FragmentShaderRGBATex
>
281 VideoStreamTextureProgram
;
282 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
283 FragmentShaderYUVVideo
> VideoYUVProgram
;
284 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
285 FragmentShaderYUVAVideo
> VideoYUVAProgram
;
287 // Special purpose / effects shaders.
288 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
290 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
292 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
295 const TileProgram
* GetTileProgram(
296 TexCoordPrecision precision
, SamplerType sampler
);
297 const TileProgramOpaque
* GetTileProgramOpaque(
298 TexCoordPrecision precision
, SamplerType sampler
);
299 const TileProgramAA
* GetTileProgramAA(
300 TexCoordPrecision precision
, SamplerType sampler
);
301 const TileProgramSwizzle
* GetTileProgramSwizzle(
302 TexCoordPrecision precision
, SamplerType sampler
);
303 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
304 TexCoordPrecision precision
, SamplerType sampler
);
305 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
306 TexCoordPrecision precision
, SamplerType sampler
);
308 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
310 const RenderPassProgram
* GetRenderPassProgram(
311 TexCoordPrecision precision
);
312 const RenderPassProgramAA
* GetRenderPassProgramAA(
313 TexCoordPrecision precision
);
314 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
315 TexCoordPrecision precision
);
316 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
317 TexCoordPrecision precision
);
318 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
319 TexCoordPrecision precision
);
320 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
321 TexCoordPrecision precision
);
322 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
323 TexCoordPrecision precision
);
324 const RenderPassMaskColorMatrixProgramAA
*
325 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
);
327 const TextureProgram
* GetTextureProgram(
328 TexCoordPrecision precision
);
329 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
330 TexCoordPrecision precision
);
331 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
332 TexCoordPrecision precision
);
333 const NonPremultipliedTextureBackgroundProgram
*
334 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
335 const TextureProgram
* GetTextureIOSurfaceProgram(
336 TexCoordPrecision precision
);
338 const VideoYUVProgram
* GetVideoYUVProgram(
339 TexCoordPrecision precision
);
340 const VideoYUVAProgram
* GetVideoYUVAProgram(
341 TexCoordPrecision precision
);
342 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
343 TexCoordPrecision precision
);
345 const DebugBorderProgram
* GetDebugBorderProgram();
346 const SolidColorProgram
* GetSolidColorProgram();
347 const SolidColorProgramAA
* GetSolidColorProgramAA();
349 TileProgram tile_program_
[NumTexCoordPrecisions
][NumSamplerTypes
];
351 tile_program_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
352 TileProgramAA tile_program_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
354 tile_program_swizzle_
[NumTexCoordPrecisions
][NumSamplerTypes
];
355 TileProgramSwizzleOpaque
356 tile_program_swizzle_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
358 tile_program_swizzle_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
360 TileCheckerboardProgram tile_checkerboard_program_
;
362 TextureProgram texture_program_
[NumTexCoordPrecisions
];
363 NonPremultipliedTextureProgram
364 nonpremultiplied_texture_program_
[NumTexCoordPrecisions
];
365 TextureBackgroundProgram texture_background_program_
[NumTexCoordPrecisions
];
366 NonPremultipliedTextureBackgroundProgram
367 nonpremultiplied_texture_background_program_
[NumTexCoordPrecisions
];
368 TextureProgram texture_io_surface_program_
[NumTexCoordPrecisions
];
370 RenderPassProgram render_pass_program_
[NumTexCoordPrecisions
];
371 RenderPassProgramAA render_pass_program_aa_
[NumTexCoordPrecisions
];
372 RenderPassMaskProgram render_pass_mask_program_
[NumTexCoordPrecisions
];
373 RenderPassMaskProgramAA render_pass_mask_program_aa_
[NumTexCoordPrecisions
];
374 RenderPassColorMatrixProgram
375 render_pass_color_matrix_program_
[NumTexCoordPrecisions
];
376 RenderPassColorMatrixProgramAA
377 render_pass_color_matrix_program_aa_
[NumTexCoordPrecisions
];
378 RenderPassMaskColorMatrixProgram
379 render_pass_mask_color_matrix_program_
[NumTexCoordPrecisions
];
380 RenderPassMaskColorMatrixProgramAA
381 render_pass_mask_color_matrix_program_aa_
[NumTexCoordPrecisions
];
383 VideoYUVProgram video_yuv_program_
[NumTexCoordPrecisions
];
384 VideoYUVAProgram video_yuva_program_
[NumTexCoordPrecisions
];
385 VideoStreamTextureProgram
386 video_stream_texture_program_
[NumTexCoordPrecisions
];
388 DebugBorderProgram debug_border_program_
;
389 SolidColorProgram solid_color_program_
;
390 SolidColorProgramAA solid_color_program_aa_
;
392 gpu::gles2::GLES2Interface
* gl_
;
393 gpu::ContextSupport
* context_support_
;
395 TextureMailboxDeleter
* texture_mailbox_deleter_
;
397 gfx::Rect swap_buffer_rect_
;
398 gfx::Rect scissor_rect_
;
400 bool is_backbuffer_discarded_
;
401 bool is_using_bind_uniform_
;
402 bool is_scissor_enabled_
;
403 bool scissor_rect_needs_reset_
;
404 bool stencil_shadow_
;
406 unsigned program_shadow_
;
407 TexturedQuadDrawCache draw_cache_
;
408 int highp_threshold_min_
;
409 int highp_threshold_cache_
;
411 struct PendingAsyncReadPixels
;
412 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
414 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
417 ScopedPtrDeque
<SyncQuery
> pending_sync_queries_
;
418 ScopedPtrDeque
<SyncQuery
> available_sync_queries_
;
419 scoped_ptr
<SyncQuery
> current_sync_query_
;
420 bool use_sync_query_
;
422 SkBitmap on_demand_tile_raster_bitmap_
;
423 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
425 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
428 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
429 // call made by the compositor. Useful for debugging rendering issues but
430 // will significantly degrade performance.
431 #define DEBUG_GL_CALLS 0
433 #if DEBUG_GL_CALLS && !defined(NDEBUG)
434 #define GLC(context, x) \
435 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
437 #define GLC(context, x) (x)
442 #endif // CC_OUTPUT_GL_RENDERER_H_