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 RendererSettings
* settings
,
53 OutputSurface
* output_surface
,
54 ResourceProvider
* resource_provider
,
55 TextureMailboxDeleter
* texture_mailbox_deleter
,
56 int highp_threshold_min
);
58 ~GLRenderer() override
;
60 const RendererCapabilitiesImpl
& Capabilities() const override
;
62 // Waits for rendering to finish.
63 void Finish() override
;
65 void DoNoOp() override
;
66 void SwapBuffers(const CompositorFrameMetadata
& metadata
) override
;
68 virtual bool IsContextLost();
70 static void DebugGLCall(gpu::gles2::GLES2Interface
* gl
,
76 GLRenderer(RendererClient
* client
,
77 const RendererSettings
* settings
,
78 OutputSurface
* output_surface
,
79 ResourceProvider
* resource_provider
,
80 TextureMailboxDeleter
* texture_mailbox_deleter
,
81 int highp_threshold_min
);
83 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 DrawingFrame
* frame
,
93 const gfx::Rect
& rect
,
94 scoped_ptr
<CopyOutputRequest
> request
);
95 void GetFramebufferTexture(unsigned texture_id
,
96 ResourceFormat texture_format
,
97 const gfx::Rect
& device_rect
);
98 void ReleaseRenderPassTextures();
100 void SetStencilEnabled(bool enabled
);
101 bool stencil_enabled() const { return stencil_shadow_
; }
102 void SetBlendEnabled(bool enabled
);
103 bool blend_enabled() const { return blend_shadow_
; }
105 void BindFramebufferToOutputSurface(DrawingFrame
* frame
) override
;
106 bool BindFramebufferToTexture(DrawingFrame
* frame
,
107 const ScopedResource
* resource
,
108 const gfx::Rect
& target_rect
) override
;
109 void SetDrawViewport(const gfx::Rect
& window_space_viewport
) override
;
110 void SetScissorTestRect(const gfx::Rect
& scissor_rect
) override
;
111 void DiscardPixels(bool has_external_stencil_test
,
112 bool draw_rect_covers_full_surface
) override
;
113 void ClearFramebuffer(DrawingFrame
* frame
,
114 bool has_external_stencil_test
) override
;
115 void DoDrawQuad(DrawingFrame
* frame
, const class DrawQuad
*) override
;
116 void BeginDrawingFrame(DrawingFrame
* frame
) override
;
117 void FinishDrawingFrame(DrawingFrame
* frame
) override
;
118 bool FlippedFramebuffer(const DrawingFrame
* frame
) const override
;
119 bool FlippedRootFramebuffer() const;
120 void EnsureScissorTestEnabled() override
;
121 void EnsureScissorTestDisabled() override
;
122 void CopyCurrentRenderPassToBitmap(
124 scoped_ptr
<CopyOutputRequest
> request
) override
;
125 void FinishDrawingQuadList() override
;
127 // Returns true if quad requires antialiasing and false otherwise.
128 static bool ShouldAntialiasQuad(const gfx::Transform
& device_transform
,
129 const DrawQuad
* quad
,
130 bool force_antialiasing
);
132 // Inflate the quad and fill edge array for fragment shader.
133 // |local_quad| is set to inflated quad. |edge| array is filled with
134 // inflated quad's edge data.
135 static void 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 static bool IsDefaultBlendMode(SkXfermode::Mode blend_mode
) {
151 return blend_mode
== SkXfermode::kSrcOver_Mode
;
153 bool CanApplyBlendModeUsingBlendFunc(SkXfermode::Mode blend_mode
);
154 void ApplyBlendModeUsingBlendFunc(SkXfermode::Mode blend_mode
);
155 void RestoreBlendFuncToDefault(SkXfermode::Mode blend_mode
);
157 gfx::Rect
GetBackdropBoundingBoxForRenderPassQuad(
159 const RenderPassDrawQuad
* quad
,
160 const gfx::Transform
& contents_device_transform
,
162 scoped_ptr
<ScopedResource
> GetBackdropTexture(const gfx::Rect
& bounding_rect
);
164 static bool ShouldApplyBackgroundFilters(DrawingFrame
* frame
,
165 const RenderPassDrawQuad
* quad
);
166 skia::RefPtr
<SkImage
> ApplyBackgroundFilters(
168 const RenderPassDrawQuad
* quad
,
169 ScopedResource
* background_texture
);
171 void DrawRenderPassQuad(DrawingFrame
* frame
, const RenderPassDrawQuad
* quad
);
172 void DrawSolidColorQuad(const DrawingFrame
* frame
,
173 const SolidColorDrawQuad
* quad
);
174 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
175 const StreamVideoDrawQuad
* quad
);
176 void EnqueueTextureQuad(const DrawingFrame
* frame
,
177 const TextureDrawQuad
* quad
);
178 void FlushTextureQuadCache();
179 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
180 const IOSurfaceDrawQuad
* quad
);
181 void DrawTileQuad(const DrawingFrame
* frame
, const TileDrawQuad
* quad
);
182 void DrawContentQuad(const DrawingFrame
* frame
,
183 const ContentDrawQuadBase
* quad
,
184 ResourceProvider::ResourceId resource_id
);
185 void DrawContentQuadAA(const DrawingFrame
* frame
,
186 const ContentDrawQuadBase
* quad
,
187 ResourceProvider::ResourceId resource_id
,
188 const gfx::Transform
& device_transform
);
189 void DrawContentQuadNoAA(const DrawingFrame
* frame
,
190 const ContentDrawQuadBase
* quad
,
191 ResourceProvider::ResourceId resource_id
);
192 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
193 const YUVVideoDrawQuad
* quad
);
194 void DrawPictureQuad(const DrawingFrame
* frame
,
195 const PictureDrawQuad
* quad
);
197 void SetShaderOpacity(float opacity
, int alpha_location
);
198 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
199 void DrawQuadGeometry(const DrawingFrame
* frame
,
200 const gfx::Transform
& draw_transform
,
201 const gfx::RectF
& quad_rect
,
202 int matrix_location
);
203 void SetUseProgram(unsigned program
);
205 bool UseScopedTexture(DrawingFrame
* frame
,
206 const ScopedResource
* resource
,
207 const gfx::Rect
& viewport_rect
);
209 bool MakeContextCurrent();
211 void InitializeSharedObjects();
212 void CleanupSharedObjects();
214 typedef base::Callback
<void(scoped_ptr
<CopyOutputRequest
> copy_request
,
216 AsyncGetFramebufferPixelsCleanupCallback
;
217 void FinishedReadback(unsigned source_buffer
,
219 const gfx::Size
& size
);
221 void ReinitializeGLState();
222 void RestoreGLState();
223 void RestoreFramebuffer(DrawingFrame
* frame
);
225 void DiscardBackbuffer() override
;
226 void EnsureBackbuffer() override
;
227 void EnforceMemoryPolicy();
229 void ScheduleOverlays(DrawingFrame
* frame
);
231 typedef ScopedPtrVector
<ResourceProvider::ScopedReadLockGL
>
232 OverlayResourceLockList
;
233 OverlayResourceLockList pending_overlay_resources_
;
234 OverlayResourceLockList in_use_overlay_resources_
;
236 RendererCapabilitiesImpl capabilities_
;
238 unsigned offscreen_framebuffer_id_
;
240 scoped_ptr
<GeometryBinding
> shared_geometry_
;
241 gfx::QuadF shared_geometry_quad_
;
243 // This block of bindings defines all of the programs used by the compositor
244 // itself. Add any new programs here to GLRendererShaderTest.
246 // Tiled layer shaders.
247 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
249 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
251 typedef ProgramBinding
<VertexShaderTileAA
,
252 FragmentShaderRGBATexClampSwizzleAlphaAA
>
253 TileProgramSwizzleAA
;
254 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
256 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
258 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
259 TileProgramSwizzleOpaque
;
260 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
261 TileCheckerboardProgram
;
264 typedef ProgramBinding
<VertexShaderPosTexTransform
,
265 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
266 typedef ProgramBinding
<VertexShaderPosTexTransform
,
267 FragmentShaderRGBATexPremultiplyAlpha
>
268 NonPremultipliedTextureProgram
;
269 typedef ProgramBinding
<VertexShaderPosTexTransform
,
270 FragmentShaderTexBackgroundVaryingAlpha
>
271 TextureBackgroundProgram
;
272 typedef ProgramBinding
<VertexShaderPosTexTransform
,
273 FragmentShaderTexBackgroundPremultiplyAlpha
>
274 NonPremultipliedTextureBackgroundProgram
;
276 // Render surface shaders.
277 typedef ProgramBinding
<VertexShaderPosTexTransform
,
278 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
279 typedef ProgramBinding
<VertexShaderPosTexTransform
,
280 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
281 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
282 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
283 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
284 FragmentShaderRGBATexAlphaMaskAA
>
285 RenderPassMaskProgramAA
;
286 typedef ProgramBinding
<VertexShaderPosTexTransform
,
287 FragmentShaderRGBATexColorMatrixAlpha
>
288 RenderPassColorMatrixProgram
;
289 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
290 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
291 RenderPassMaskColorMatrixProgramAA
;
292 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
293 FragmentShaderRGBATexAlphaColorMatrixAA
>
294 RenderPassColorMatrixProgramAA
;
295 typedef ProgramBinding
<VertexShaderPosTexTransform
,
296 FragmentShaderRGBATexAlphaMaskColorMatrix
>
297 RenderPassMaskColorMatrixProgram
;
300 typedef ProgramBinding
<VertexShaderVideoTransform
, FragmentShaderRGBATex
>
301 VideoStreamTextureProgram
;
302 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
303 FragmentShaderYUVVideo
> VideoYUVProgram
;
304 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
305 FragmentShaderYUVAVideo
> VideoYUVAProgram
;
307 // Special purpose / effects shaders.
308 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
310 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
312 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
315 const TileProgram
* GetTileProgram(
316 TexCoordPrecision precision
, SamplerType sampler
);
317 const TileProgramOpaque
* GetTileProgramOpaque(
318 TexCoordPrecision precision
, SamplerType sampler
);
319 const TileProgramAA
* GetTileProgramAA(
320 TexCoordPrecision precision
, SamplerType sampler
);
321 const TileProgramSwizzle
* GetTileProgramSwizzle(
322 TexCoordPrecision precision
, SamplerType sampler
);
323 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
324 TexCoordPrecision precision
, SamplerType sampler
);
325 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
326 TexCoordPrecision precision
, SamplerType sampler
);
328 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
330 const RenderPassProgram
* GetRenderPassProgram(TexCoordPrecision precision
,
331 BlendMode blend_mode
);
332 const RenderPassProgramAA
* GetRenderPassProgramAA(TexCoordPrecision precision
,
333 BlendMode blend_mode
);
334 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
335 TexCoordPrecision precision
,
337 BlendMode blend_mode
);
338 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
339 TexCoordPrecision precision
,
341 BlendMode blend_mode
);
342 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
343 TexCoordPrecision precision
,
344 BlendMode blend_mode
);
345 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
346 TexCoordPrecision precision
,
347 BlendMode blend_mode
);
348 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
349 TexCoordPrecision precision
,
351 BlendMode blend_mode
);
352 const RenderPassMaskColorMatrixProgramAA
*
353 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
,
355 BlendMode blend_mode
);
357 const TextureProgram
* GetTextureProgram(
358 TexCoordPrecision precision
);
359 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
360 TexCoordPrecision precision
);
361 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
362 TexCoordPrecision precision
);
363 const NonPremultipliedTextureBackgroundProgram
*
364 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
365 const TextureProgram
* GetTextureIOSurfaceProgram(
366 TexCoordPrecision precision
);
368 const VideoYUVProgram
* GetVideoYUVProgram(
369 TexCoordPrecision precision
);
370 const VideoYUVAProgram
* GetVideoYUVAProgram(
371 TexCoordPrecision precision
);
372 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
373 TexCoordPrecision precision
);
375 const DebugBorderProgram
* GetDebugBorderProgram();
376 const SolidColorProgram
* GetSolidColorProgram();
377 const SolidColorProgramAA
* GetSolidColorProgramAA();
379 TileProgram tile_program_
[NumTexCoordPrecisions
][NumSamplerTypes
];
381 tile_program_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
382 TileProgramAA tile_program_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
384 tile_program_swizzle_
[NumTexCoordPrecisions
][NumSamplerTypes
];
385 TileProgramSwizzleOpaque
386 tile_program_swizzle_opaque_
[NumTexCoordPrecisions
][NumSamplerTypes
];
388 tile_program_swizzle_aa_
[NumTexCoordPrecisions
][NumSamplerTypes
];
390 TileCheckerboardProgram tile_checkerboard_program_
;
392 TextureProgram texture_program_
[NumTexCoordPrecisions
];
393 NonPremultipliedTextureProgram
394 nonpremultiplied_texture_program_
[NumTexCoordPrecisions
];
395 TextureBackgroundProgram texture_background_program_
[NumTexCoordPrecisions
];
396 NonPremultipliedTextureBackgroundProgram
397 nonpremultiplied_texture_background_program_
[NumTexCoordPrecisions
];
398 TextureProgram texture_io_surface_program_
[NumTexCoordPrecisions
];
400 RenderPassProgram render_pass_program_
[NumTexCoordPrecisions
][NumBlendModes
];
402 render_pass_program_aa_
[NumTexCoordPrecisions
][NumBlendModes
];
403 RenderPassMaskProgram render_pass_mask_program_
404 [NumTexCoordPrecisions
][NumSamplerTypes
][NumBlendModes
];
405 RenderPassMaskProgramAA render_pass_mask_program_aa_
406 [NumTexCoordPrecisions
][NumSamplerTypes
][NumBlendModes
];
407 RenderPassColorMatrixProgram
408 render_pass_color_matrix_program_
[NumTexCoordPrecisions
][NumBlendModes
];
409 RenderPassColorMatrixProgramAA render_pass_color_matrix_program_aa_
410 [NumTexCoordPrecisions
][NumBlendModes
];
411 RenderPassMaskColorMatrixProgram render_pass_mask_color_matrix_program_
412 [NumTexCoordPrecisions
][NumSamplerTypes
][NumBlendModes
];
413 RenderPassMaskColorMatrixProgramAA render_pass_mask_color_matrix_program_aa_
414 [NumTexCoordPrecisions
][NumSamplerTypes
][NumBlendModes
];
416 VideoYUVProgram video_yuv_program_
[NumTexCoordPrecisions
];
417 VideoYUVAProgram video_yuva_program_
[NumTexCoordPrecisions
];
418 VideoStreamTextureProgram
419 video_stream_texture_program_
[NumTexCoordPrecisions
];
421 DebugBorderProgram debug_border_program_
;
422 SolidColorProgram solid_color_program_
;
423 SolidColorProgramAA solid_color_program_aa_
;
425 gpu::gles2::GLES2Interface
* gl_
;
426 gpu::ContextSupport
* context_support_
;
428 TextureMailboxDeleter
* texture_mailbox_deleter_
;
430 gfx::Rect swap_buffer_rect_
;
431 gfx::Rect scissor_rect_
;
433 bool is_backbuffer_discarded_
;
434 bool is_using_bind_uniform_
;
435 bool is_scissor_enabled_
;
436 bool scissor_rect_needs_reset_
;
437 bool stencil_shadow_
;
439 unsigned program_shadow_
;
440 TexturedQuadDrawCache draw_cache_
;
441 int highp_threshold_min_
;
442 int highp_threshold_cache_
;
444 struct PendingAsyncReadPixels
;
445 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
447 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
450 ScopedPtrDeque
<SyncQuery
> pending_sync_queries_
;
451 ScopedPtrDeque
<SyncQuery
> available_sync_queries_
;
452 scoped_ptr
<SyncQuery
> current_sync_query_
;
453 bool use_sync_query_
;
454 bool use_blend_equation_advanced_
;
455 bool use_blend_equation_advanced_coherent_
;
457 SkBitmap on_demand_tile_raster_bitmap_
;
458 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
460 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
463 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
464 // call made by the compositor. Useful for debugging rendering issues but
465 // will significantly degrade performance.
466 #define DEBUG_GL_CALLS 0
468 #if DEBUG_GL_CALLS && !defined(NDEBUG)
469 #define GLC(context, x) \
470 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
472 #define GLC(context, x) (x)
477 #endif // CC_OUTPUT_GL_RENDERER_H_