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 StaticGeometryBinding
;
43 class DynamicGeometryBinding
;
44 class ScopedEnsureFramebufferAllocation
;
46 // Class that handles drawing of composited render layers using GL.
47 class CC_EXPORT GLRenderer
: public DirectRenderer
{
49 class ScopedUseGrContext
;
51 static scoped_ptr
<GLRenderer
> Create(
52 RendererClient
* client
,
53 const RendererSettings
* settings
,
54 OutputSurface
* output_surface
,
55 ResourceProvider
* resource_provider
,
56 TextureMailboxDeleter
* texture_mailbox_deleter
,
57 int highp_threshold_min
);
59 ~GLRenderer() override
;
61 const RendererCapabilitiesImpl
& Capabilities() const override
;
63 // Waits for rendering to finish.
64 void Finish() override
;
66 void DoNoOp() override
;
67 void SwapBuffers(const CompositorFrameMetadata
& metadata
) override
;
69 virtual bool IsContextLost();
72 GLRenderer(RendererClient
* client
,
73 const RendererSettings
* settings
,
74 OutputSurface
* output_surface
,
75 ResourceProvider
* resource_provider
,
76 TextureMailboxDeleter
* texture_mailbox_deleter
,
77 int highp_threshold_min
);
79 void DidChangeVisibility() override
;
81 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_
; }
83 const gfx::QuadF
& SharedGeometryQuad() const { return shared_geometry_quad_
; }
84 const StaticGeometryBinding
* SharedGeometry() const {
85 return shared_geometry_
.get();
88 void GetFramebufferPixelsAsync(const DrawingFrame
* frame
,
89 const gfx::Rect
& rect
,
90 scoped_ptr
<CopyOutputRequest
> request
);
91 void GetFramebufferTexture(unsigned texture_id
,
92 ResourceFormat texture_format
,
93 const gfx::Rect
& device_rect
);
94 void ReleaseRenderPassTextures();
95 enum BoundGeometry
{ NO_BINDING
, SHARED_BINDING
, CLIPPED_BINDING
};
96 void PrepareGeometry(BoundGeometry geometry_to_bind
);
97 void SetStencilEnabled(bool enabled
);
98 bool stencil_enabled() const { return stencil_shadow_
; }
99 void SetBlendEnabled(bool enabled
);
100 bool blend_enabled() const { return blend_shadow_
; }
102 void BindFramebufferToOutputSurface(DrawingFrame
* frame
) override
;
103 bool BindFramebufferToTexture(DrawingFrame
* frame
,
104 const ScopedResource
* resource
,
105 const gfx::Rect
& target_rect
) override
;
106 void SetScissorTestRect(const gfx::Rect
& scissor_rect
) override
;
107 void PrepareSurfaceForPass(DrawingFrame
* frame
,
108 SurfaceInitializationMode initialization_mode
,
109 const gfx::Rect
& render_pass_scissor
) override
;
110 void DoDrawQuad(DrawingFrame
* frame
,
111 const class DrawQuad
*,
112 const gfx::QuadF
* draw_region
) override
;
113 void BeginDrawingFrame(DrawingFrame
* frame
) override
;
114 void FinishDrawingFrame(DrawingFrame
* frame
) override
;
115 bool FlippedFramebuffer(const DrawingFrame
* frame
) const override
;
116 bool FlippedRootFramebuffer() const;
117 void EnsureScissorTestEnabled() override
;
118 void EnsureScissorTestDisabled() override
;
119 void CopyCurrentRenderPassToBitmap(
121 scoped_ptr
<CopyOutputRequest
> request
) override
;
122 void FinishDrawingQuadList() override
;
124 // Returns true if quad requires antialiasing and false otherwise.
125 static bool ShouldAntialiasQuad(const gfx::Transform
& device_transform
,
126 const DrawQuad
* quad
,
127 bool force_antialiasing
);
129 // Inflate the quad and fill edge array for fragment shader.
130 // |local_quad| is set to inflated quad. |edge| array is filled with
131 // inflated quad's edge data.
132 static void SetupQuadForClippingAndAntialiasing(
133 const gfx::Transform
& device_transform
,
134 const DrawQuad
* quad
,
136 const gfx::QuadF
* clip_region
,
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 DiscardPixels();
147 void ClearFramebuffer(DrawingFrame
* frame
);
150 void DrawCheckerboardQuad(const DrawingFrame
* frame
,
151 const CheckerboardDrawQuad
* quad
,
152 const gfx::QuadF
* clip_region
);
153 void DrawDebugBorderQuad(const DrawingFrame
* frame
,
154 const DebugBorderDrawQuad
* quad
);
155 static bool IsDefaultBlendMode(SkXfermode::Mode blend_mode
) {
156 return blend_mode
== SkXfermode::kSrcOver_Mode
;
158 bool CanApplyBlendModeUsingBlendFunc(SkXfermode::Mode blend_mode
);
159 void ApplyBlendModeUsingBlendFunc(SkXfermode::Mode blend_mode
);
160 void RestoreBlendFuncToDefault(SkXfermode::Mode blend_mode
);
162 gfx::Rect
GetBackdropBoundingBoxForRenderPassQuad(
164 const RenderPassDrawQuad
* quad
,
165 const gfx::Transform
& contents_device_transform
,
166 const gfx::QuadF
* clip_region
,
168 scoped_ptr
<ScopedResource
> GetBackdropTexture(const gfx::Rect
& bounding_rect
);
170 static bool ShouldApplyBackgroundFilters(DrawingFrame
* frame
,
171 const RenderPassDrawQuad
* quad
);
172 skia::RefPtr
<SkImage
> ApplyBackgroundFilters(
174 const RenderPassDrawQuad
* quad
,
175 ScopedResource
* background_texture
);
177 void DrawRenderPassQuad(DrawingFrame
* frame
,
178 const RenderPassDrawQuad
* quadi
,
179 const gfx::QuadF
* clip_region
);
180 void DrawSolidColorQuad(const DrawingFrame
* frame
,
181 const SolidColorDrawQuad
* quad
,
182 const gfx::QuadF
* clip_region
);
183 void DrawStreamVideoQuad(const DrawingFrame
* frame
,
184 const StreamVideoDrawQuad
* quad
,
185 const gfx::QuadF
* clip_region
);
186 void DrawTextureQuad(const DrawingFrame
* frame
,
187 const TextureDrawQuad
* quad
,
188 const gfx::QuadF
* clip_region
);
189 void EnqueueTextureQuad(const DrawingFrame
* frame
,
190 const TextureDrawQuad
* quad
,
191 const gfx::QuadF
* clip_region
);
192 void FlushTextureQuadCache(BoundGeometry flush_binding
);
193 void DrawIOSurfaceQuad(const DrawingFrame
* frame
,
194 const IOSurfaceDrawQuad
* quad
,
195 const gfx::QuadF
* clip_region
);
196 void DrawTileQuad(const DrawingFrame
* frame
,
197 const TileDrawQuad
* quad
,
198 const gfx::QuadF
* clip_region
);
199 void DrawContentQuad(const DrawingFrame
* frame
,
200 const ContentDrawQuadBase
* quad
,
201 ResourceProvider::ResourceId resource_id
,
202 const gfx::QuadF
* clip_region
);
203 void DrawContentQuadAA(const DrawingFrame
* frame
,
204 const ContentDrawQuadBase
* quad
,
205 ResourceProvider::ResourceId resource_id
,
206 const gfx::Transform
& device_transform
,
207 const gfx::QuadF
* clip_region
);
208 void DrawContentQuadNoAA(const DrawingFrame
* frame
,
209 const ContentDrawQuadBase
* quad
,
210 ResourceProvider::ResourceId resource_id
,
211 const gfx::QuadF
* clip_region
);
212 void DrawYUVVideoQuad(const DrawingFrame
* frame
,
213 const YUVVideoDrawQuad
* quad
,
214 const gfx::QuadF
* clip_region
);
215 void DrawPictureQuad(const DrawingFrame
* frame
,
216 const PictureDrawQuad
* quad
,
217 const gfx::QuadF
* clip_region
);
219 void SetShaderOpacity(float opacity
, int alpha_location
);
220 void SetShaderQuadF(const gfx::QuadF
& quad
, int quad_location
);
221 void DrawQuadGeometryClippedByQuadF(const DrawingFrame
* frame
,
222 const gfx::Transform
& draw_transform
,
223 const gfx::RectF
& quad_rect
,
224 const gfx::QuadF
& clipping_region_quad
,
227 void DrawQuadGeometry(const DrawingFrame
* frame
,
228 const gfx::Transform
& draw_transform
,
229 const gfx::RectF
& quad_rect
,
230 int matrix_location
);
231 void SetUseProgram(unsigned program
);
233 bool UseScopedTexture(DrawingFrame
* frame
,
234 const ScopedResource
* resource
,
235 const gfx::Rect
& viewport_rect
);
237 bool MakeContextCurrent();
239 void InitializeSharedObjects();
240 void CleanupSharedObjects();
242 typedef base::Callback
<void(scoped_ptr
<CopyOutputRequest
> copy_request
,
244 AsyncGetFramebufferPixelsCleanupCallback
;
245 void FinishedReadback(unsigned source_buffer
,
247 const gfx::Size
& size
);
249 void ReinitializeGLState();
250 void RestoreGLState();
251 void RestoreFramebuffer(DrawingFrame
* frame
);
253 void DiscardBackbuffer() override
;
254 void EnsureBackbuffer() override
;
255 void EnforceMemoryPolicy();
257 void ScheduleOverlays(DrawingFrame
* frame
);
259 typedef ScopedPtrVector
<ResourceProvider::ScopedReadLockGL
>
260 OverlayResourceLockList
;
261 OverlayResourceLockList pending_overlay_resources_
;
262 OverlayResourceLockList in_use_overlay_resources_
;
264 RendererCapabilitiesImpl capabilities_
;
266 unsigned offscreen_framebuffer_id_
;
268 scoped_ptr
<StaticGeometryBinding
> shared_geometry_
;
269 scoped_ptr
<DynamicGeometryBinding
> clipped_geometry_
;
270 gfx::QuadF shared_geometry_quad_
;
272 // This block of bindings defines all of the programs used by the compositor
273 // itself. Add any new programs here to GLRendererShaderTest.
275 // Tiled layer shaders.
276 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexAlpha
>
278 typedef ProgramBinding
<VertexShaderTileAA
, FragmentShaderRGBATexClampAlphaAA
>
280 typedef ProgramBinding
<VertexShaderTileAA
,
281 FragmentShaderRGBATexClampSwizzleAlphaAA
>
282 TileProgramSwizzleAA
;
283 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexOpaque
>
285 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleAlpha
>
287 typedef ProgramBinding
<VertexShaderTile
, FragmentShaderRGBATexSwizzleOpaque
>
288 TileProgramSwizzleOpaque
;
289 typedef ProgramBinding
<VertexShaderPosTex
, FragmentShaderCheckerboard
>
290 TileCheckerboardProgram
;
293 typedef ProgramBinding
<VertexShaderPosTexTransform
,
294 FragmentShaderRGBATexVaryingAlpha
> TextureProgram
;
295 typedef ProgramBinding
<VertexShaderPosTexTransform
,
296 FragmentShaderRGBATexPremultiplyAlpha
>
297 NonPremultipliedTextureProgram
;
298 typedef ProgramBinding
<VertexShaderPosTexTransform
,
299 FragmentShaderTexBackgroundVaryingAlpha
>
300 TextureBackgroundProgram
;
301 typedef ProgramBinding
<VertexShaderPosTexTransform
,
302 FragmentShaderTexBackgroundPremultiplyAlpha
>
303 NonPremultipliedTextureBackgroundProgram
;
305 // Render surface shaders.
306 typedef ProgramBinding
<VertexShaderPosTexTransform
,
307 FragmentShaderRGBATexAlpha
> RenderPassProgram
;
308 typedef ProgramBinding
<VertexShaderPosTexTransform
,
309 FragmentShaderRGBATexAlphaMask
> RenderPassMaskProgram
;
310 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
311 FragmentShaderRGBATexAlphaAA
> RenderPassProgramAA
;
312 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
313 FragmentShaderRGBATexAlphaMaskAA
>
314 RenderPassMaskProgramAA
;
315 typedef ProgramBinding
<VertexShaderPosTexTransform
,
316 FragmentShaderRGBATexColorMatrixAlpha
>
317 RenderPassColorMatrixProgram
;
318 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
319 FragmentShaderRGBATexAlphaMaskColorMatrixAA
>
320 RenderPassMaskColorMatrixProgramAA
;
321 typedef ProgramBinding
<VertexShaderQuadTexTransformAA
,
322 FragmentShaderRGBATexAlphaColorMatrixAA
>
323 RenderPassColorMatrixProgramAA
;
324 typedef ProgramBinding
<VertexShaderPosTexTransform
,
325 FragmentShaderRGBATexAlphaMaskColorMatrix
>
326 RenderPassMaskColorMatrixProgram
;
329 typedef ProgramBinding
<VertexShaderVideoTransform
, FragmentShaderRGBATex
>
330 VideoStreamTextureProgram
;
331 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
332 FragmentShaderYUVVideo
> VideoYUVProgram
;
333 typedef ProgramBinding
<VertexShaderPosTexYUVStretchOffset
,
334 FragmentShaderYUVAVideo
> VideoYUVAProgram
;
336 // Special purpose / effects shaders.
337 typedef ProgramBinding
<VertexShaderPos
, FragmentShaderColor
>
339 typedef ProgramBinding
<VertexShaderQuad
, FragmentShaderColor
>
341 typedef ProgramBinding
<VertexShaderQuadAA
, FragmentShaderColorAA
>
344 const TileProgram
* GetTileProgram(
345 TexCoordPrecision precision
, SamplerType sampler
);
346 const TileProgramOpaque
* GetTileProgramOpaque(
347 TexCoordPrecision precision
, SamplerType sampler
);
348 const TileProgramAA
* GetTileProgramAA(
349 TexCoordPrecision precision
, SamplerType sampler
);
350 const TileProgramSwizzle
* GetTileProgramSwizzle(
351 TexCoordPrecision precision
, SamplerType sampler
);
352 const TileProgramSwizzleOpaque
* GetTileProgramSwizzleOpaque(
353 TexCoordPrecision precision
, SamplerType sampler
);
354 const TileProgramSwizzleAA
* GetTileProgramSwizzleAA(
355 TexCoordPrecision precision
, SamplerType sampler
);
357 const TileCheckerboardProgram
* GetTileCheckerboardProgram();
359 const RenderPassProgram
* GetRenderPassProgram(TexCoordPrecision precision
,
360 BlendMode blend_mode
);
361 const RenderPassProgramAA
* GetRenderPassProgramAA(TexCoordPrecision precision
,
362 BlendMode blend_mode
);
363 const RenderPassMaskProgram
* GetRenderPassMaskProgram(
364 TexCoordPrecision precision
,
366 BlendMode blend_mode
,
367 bool mask_for_background
);
368 const RenderPassMaskProgramAA
* GetRenderPassMaskProgramAA(
369 TexCoordPrecision precision
,
371 BlendMode blend_mode
,
372 bool mask_for_background
);
373 const RenderPassColorMatrixProgram
* GetRenderPassColorMatrixProgram(
374 TexCoordPrecision precision
,
375 BlendMode blend_mode
);
376 const RenderPassColorMatrixProgramAA
* GetRenderPassColorMatrixProgramAA(
377 TexCoordPrecision precision
,
378 BlendMode blend_mode
);
379 const RenderPassMaskColorMatrixProgram
* GetRenderPassMaskColorMatrixProgram(
380 TexCoordPrecision precision
,
382 BlendMode blend_mode
,
383 bool mask_for_background
);
384 const RenderPassMaskColorMatrixProgramAA
*
385 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision
,
387 BlendMode blend_mode
,
388 bool mask_for_background
);
390 const TextureProgram
* GetTextureProgram(
391 TexCoordPrecision precision
);
392 const NonPremultipliedTextureProgram
* GetNonPremultipliedTextureProgram(
393 TexCoordPrecision precision
);
394 const TextureBackgroundProgram
* GetTextureBackgroundProgram(
395 TexCoordPrecision precision
);
396 const NonPremultipliedTextureBackgroundProgram
*
397 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision
);
398 const TextureProgram
* GetTextureIOSurfaceProgram(
399 TexCoordPrecision precision
);
401 const VideoYUVProgram
* GetVideoYUVProgram(
402 TexCoordPrecision precision
);
403 const VideoYUVAProgram
* GetVideoYUVAProgram(
404 TexCoordPrecision precision
);
405 const VideoStreamTextureProgram
* GetVideoStreamTextureProgram(
406 TexCoordPrecision precision
);
408 const DebugBorderProgram
* GetDebugBorderProgram();
409 const SolidColorProgram
* GetSolidColorProgram();
410 const SolidColorProgramAA
* GetSolidColorProgramAA();
413 tile_program_
[LAST_TEX_COORD_PRECISION
+ 1][LAST_SAMPLER_TYPE
+ 1];
415 tile_program_opaque_
[LAST_TEX_COORD_PRECISION
+ 1][LAST_SAMPLER_TYPE
+ 1];
417 tile_program_aa_
[LAST_TEX_COORD_PRECISION
+ 1][LAST_SAMPLER_TYPE
+ 1];
418 TileProgramSwizzle tile_program_swizzle_
[LAST_TEX_COORD_PRECISION
+
419 1][LAST_SAMPLER_TYPE
+ 1];
420 TileProgramSwizzleOpaque
421 tile_program_swizzle_opaque_
[LAST_TEX_COORD_PRECISION
+
422 1][LAST_SAMPLER_TYPE
+ 1];
423 TileProgramSwizzleAA tile_program_swizzle_aa_
[LAST_TEX_COORD_PRECISION
+
424 1][LAST_SAMPLER_TYPE
+ 1];
426 TileCheckerboardProgram tile_checkerboard_program_
;
428 TextureProgram texture_program_
[LAST_TEX_COORD_PRECISION
+ 1];
429 NonPremultipliedTextureProgram
430 nonpremultiplied_texture_program_
[LAST_TEX_COORD_PRECISION
+ 1];
431 TextureBackgroundProgram
432 texture_background_program_
[LAST_TEX_COORD_PRECISION
+ 1];
433 NonPremultipliedTextureBackgroundProgram
434 nonpremultiplied_texture_background_program_
[LAST_TEX_COORD_PRECISION
+
436 TextureProgram texture_io_surface_program_
[LAST_TEX_COORD_PRECISION
+ 1];
439 render_pass_program_
[LAST_TEX_COORD_PRECISION
+ 1][LAST_BLEND_MODE
+ 1];
440 RenderPassProgramAA render_pass_program_aa_
[LAST_TEX_COORD_PRECISION
+
441 1][LAST_BLEND_MODE
+ 1];
442 RenderPassMaskProgram
443 render_pass_mask_program_
[LAST_TEX_COORD_PRECISION
+ 1]
444 [LAST_SAMPLER_TYPE
+ 1]
445 [LAST_BLEND_MODE
+ 1]
446 [LAST_MASK_VALUE
+ 1];
447 RenderPassMaskProgramAA
448 render_pass_mask_program_aa_
[LAST_TEX_COORD_PRECISION
+ 1]
449 [LAST_SAMPLER_TYPE
+ 1]
450 [LAST_BLEND_MODE
+ 1]
451 [LAST_MASK_VALUE
+ 1];
452 RenderPassColorMatrixProgram
453 render_pass_color_matrix_program_
[LAST_TEX_COORD_PRECISION
+
454 1][LAST_BLEND_MODE
+ 1];
455 RenderPassColorMatrixProgramAA
456 render_pass_color_matrix_program_aa_
[LAST_TEX_COORD_PRECISION
+
457 1][LAST_BLEND_MODE
+ 1];
458 RenderPassMaskColorMatrixProgram
459 render_pass_mask_color_matrix_program_
[LAST_TEX_COORD_PRECISION
+ 1]
460 [LAST_SAMPLER_TYPE
+ 1]
461 [LAST_BLEND_MODE
+ 1]
462 [LAST_MASK_VALUE
+ 1];
463 RenderPassMaskColorMatrixProgramAA
464 render_pass_mask_color_matrix_program_aa_
[LAST_TEX_COORD_PRECISION
+ 1]
465 [LAST_SAMPLER_TYPE
+ 1]
466 [LAST_BLEND_MODE
+ 1]
467 [LAST_MASK_VALUE
+ 1];
469 VideoYUVProgram video_yuv_program_
[LAST_TEX_COORD_PRECISION
+ 1];
470 VideoYUVAProgram video_yuva_program_
[LAST_TEX_COORD_PRECISION
+ 1];
471 VideoStreamTextureProgram
472 video_stream_texture_program_
[LAST_TEX_COORD_PRECISION
+ 1];
474 DebugBorderProgram debug_border_program_
;
475 SolidColorProgram solid_color_program_
;
476 SolidColorProgramAA solid_color_program_aa_
;
478 gpu::gles2::GLES2Interface
* gl_
;
479 gpu::ContextSupport
* context_support_
;
481 TextureMailboxDeleter
* texture_mailbox_deleter_
;
483 gfx::Rect swap_buffer_rect_
;
484 gfx::Rect scissor_rect_
;
485 bool is_backbuffer_discarded_
;
486 bool is_using_bind_uniform_
;
487 bool is_scissor_enabled_
;
488 bool scissor_rect_needs_reset_
;
489 bool stencil_shadow_
;
491 unsigned program_shadow_
;
492 TexturedQuadDrawCache draw_cache_
;
493 int highp_threshold_min_
;
494 int highp_threshold_cache_
;
496 struct PendingAsyncReadPixels
;
497 ScopedPtrVector
<PendingAsyncReadPixels
> pending_async_read_pixels_
;
499 scoped_ptr
<ResourceProvider::ScopedWriteLockGL
> current_framebuffer_lock_
;
502 ScopedPtrDeque
<SyncQuery
> pending_sync_queries_
;
503 ScopedPtrDeque
<SyncQuery
> available_sync_queries_
;
504 scoped_ptr
<SyncQuery
> current_sync_query_
;
505 bool use_sync_query_
;
506 bool use_blend_equation_advanced_
;
507 bool use_blend_equation_advanced_coherent_
;
509 SkBitmap on_demand_tile_raster_bitmap_
;
510 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_
;
511 BoundGeometry bound_geometry_
;
512 DISALLOW_COPY_AND_ASSIGN(GLRenderer
);
517 #endif // CC_OUTPUT_GL_RENDERER_H_