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. 4 5 #ifndef CC_OUTPUT_GL_RENDERER_H_ 6 #define CC_OUTPUT_GL_RENDERER_H_ 7 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" 23 24 class SkBitmap; 25 26 namespace WebKit { class WebGraphicsContext3D; } 27 28 namespace cc { 29 30 class GLRendererShaderTest; 31 class OutputSurface; 32 class PictureDrawQuad; 33 class ScopedResource; 34 class StreamVideoDrawQuad; 35 class TextureDrawQuad; 36 class GeometryBinding; 37 class ScopedEnsureFramebufferAllocation; 38 39 // Class that handles drawing of composited render layers using GL. 40 class CC_EXPORT GLRenderer : public DirectRenderer { 41 public: 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); 47 48 virtual ~GLRenderer(); 49 50 virtual const RendererCapabilities& Capabilities() const OVERRIDE; 51 52 WebKit::WebGraphicsContext3D* Context(); 53 54 virtual void ViewportChanged() OVERRIDE; 55 56 // Waits for rendering to finish. 57 virtual void Finish() OVERRIDE; 58 59 virtual void DoNoOp() OVERRIDE; 60 virtual void SwapBuffers() OVERRIDE; 61 62 virtual void GetFramebufferPixels(void* pixels, gfx::Rect rect) OVERRIDE; 63 64 virtual bool IsContextLost() OVERRIDE; 65 66 virtual void SetVisible(bool visible) OVERRIDE; 67 68 virtual void SendManagedMemoryStats(size_t bytes_visible, 69 size_t bytes_visible_and_nearby, 70 size_t bytes_allocated) OVERRIDE; 71 72 virtual void SetDiscardBackBufferWhenNotVisible(bool discard) OVERRIDE; 73 74 static void DebugGLCall(WebKit::WebGraphicsContext3D* context, 75 const char* command, 76 const char* file, 77 int line); 78 79 bool CanUseSkiaGPUBackend() const; 80 void LazyLabelOffscreenContext(); 81 82 protected: 83 GLRenderer(RendererClient* client, 84 OutputSurface* output_surface, 85 ResourceProvider* resource_provider, 86 int highp_threshold_min); 87 88 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; } 89 bool Initialize(); 90 void InitializeGrContext(); 91 92 const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; } 93 const GeometryBinding* SharedGeometry() const { 94 return shared_geometry_.get(); 95 } 96 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(); 103 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_; } 108 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( 123 DrawingFrame* frame, 124 scoped_ptr<CopyOutputRequest> request) OVERRIDE; 125 virtual void FinishDrawingQuadList() OVERRIDE; 126 127 private: 128 friend class GLRendererShaderPixelTest; 129 friend class GLRendererShaderTest; 130 131 static void ToGLMatrix(float* gl_matrix, const gfx::Transform& transform); 132 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( 138 DrawingFrame* frame, 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); 162 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); 170 171 void CopyTextureToFramebuffer(const DrawingFrame* frame, 172 int texture_id, 173 gfx::Rect rect, 174 const gfx::Transform& draw_matrix, 175 bool flip_vertically); 176 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; 187 188 bool UseScopedTexture(DrawingFrame* frame, 189 const ScopedResource* resource, 190 gfx::Rect viewport_rect); 191 192 bool MakeContextCurrent(); 193 194 bool InitializeSharedObjects(); 195 void CleanupSharedObjects(); 196 197 typedef base::Callback<void(scoped_ptr<CopyOutputRequest> copy_request, 198 bool success)> 199 AsyncGetFramebufferPixelsCleanupCallback; 200 void DoGetFramebufferPixels( 201 uint8* pixels, 202 gfx::Rect window_rect, 203 const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback); 204 void FinishedReadback( 205 const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback, 206 unsigned source_buffer, 207 unsigned query, 208 uint8_t* dest_pixels, 209 gfx::Size size); 210 void PassOnSkBitmap(scoped_ptr<SkBitmap> bitmap, 211 scoped_ptr<SkAutoLockPixels> lock, 212 scoped_ptr<CopyOutputRequest> request, 213 bool success); 214 215 static void DeleteTextureReleaseCallback( 216 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 217 base::WeakPtr<GLRenderer> gl_renderer, 218 unsigned texture_id, 219 unsigned sync_point, 220 bool lost_resource); 221 void DeleteTextureReleaseCallbackOnImplThread(unsigned texture_id, 222 unsigned sync_point, 223 bool lost_resource); 224 225 void ReinitializeGrCanvas(); 226 void ReinitializeGLState(); 227 228 virtual void DiscardBackbuffer() OVERRIDE; 229 virtual void EnsureBackbuffer() OVERRIDE; 230 void EnforceMemoryPolicy(); 231 232 RendererCapabilities capabilities_; 233 234 unsigned offscreen_framebuffer_id_; 235 236 scoped_ptr<GeometryBinding> shared_geometry_; 237 gfx::QuadF shared_geometry_quad_; 238 239 // This block of bindings defines all of the programs used by the compositor 240 // itself. Add any new programs here to GLRendererShaderTest. 241 242 // Tiled layer shaders. 243 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha> 244 TileProgram; 245 typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA> 246 TileProgramAA; 247 typedef ProgramBinding<VertexShaderTileAA, 248 FragmentShaderRGBATexClampSwizzleAlphaAA> 249 TileProgramSwizzleAA; 250 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque> 251 TileProgramOpaque; 252 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha> 253 TileProgramSwizzle; 254 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque> 255 TileProgramSwizzleOpaque; 256 typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard> 257 TileCheckerboardProgram; 258 259 // Texture shaders. 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; 274 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; 297 298 // Video shaders. 299 typedef ProgramBinding<VertexShaderVideoTransform, 300 FragmentShaderOESImageExternal> 301 VideoStreamTextureProgram; 302 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo> 303 VideoYUVProgram; 304 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVAVideo> 305 VideoYUVAProgram; 306 307 // Special purpose / effects shaders. 308 typedef ProgramBinding<VertexShaderPos, FragmentShaderColor> 309 DebugBorderProgram; 310 typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor> 311 SolidColorProgram; 312 typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA> 313 SolidColorProgramAA; 314 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(); 324 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); 341 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); 352 353 const VideoYUVProgram* GetVideoYUVProgram( 354 TexCoordPrecision precision); 355 const VideoYUVAProgram* GetVideoYUVAProgram( 356 TexCoordPrecision precision); 357 const VideoStreamTextureProgram* GetVideoStreamTextureProgram( 358 TexCoordPrecision precision); 359 360 const DebugBorderProgram* GetDebugBorderProgram(); 361 const SolidColorProgram* GetSolidColorProgram(); 362 const SolidColorProgramAA* GetSolidColorProgramAA(); 363 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_; 371 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_; 378 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_; 385 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_; 393 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_; 405 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_; 418 419 scoped_ptr<VideoYUVProgram> video_yuv_program_; 420 scoped_ptr<VideoYUVAProgram> video_yuva_program_; 421 scoped_ptr<VideoStreamTextureProgram> video_stream_texture_program_; 422 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_; 426 427 scoped_ptr<DebugBorderProgram> debug_border_program_; 428 scoped_ptr<SolidColorProgram> solid_color_program_; 429 scoped_ptr<SolidColorProgramAA> solid_color_program_aa_; 430 431 WebKit::WebGraphicsContext3D* context_; 432 433 skia::RefPtr<GrContext> gr_context_; 434 skia::RefPtr<SkCanvas> sk_canvas_; 435 436 gfx::Rect swap_buffer_rect_; 437 gfx::Rect scissor_rect_; 438 gfx::Rect viewport_; 439 bool is_backbuffer_discarded_; 440 bool discard_backbuffer_when_not_visible_; 441 bool is_using_bind_uniform_; 442 bool visible_; 443 bool is_scissor_enabled_; 444 bool stencil_shadow_; 445 bool blend_shadow_; 446 unsigned program_shadow_; 447 TexturedQuadDrawCache draw_cache_; 448 int highp_threshold_min_; 449 int highp_threshold_cache_; 450 bool offscreen_context_labelled_; 451 452 struct PendingAsyncReadPixels; 453 ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_; 454 455 scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_; 456 457 scoped_refptr<ResourceProvider::Fence> last_swap_fence_; 458 459 SkBitmap on_demand_tile_raster_bitmap_; 460 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_; 461 462 base::WeakPtrFactory<GLRenderer> weak_factory_; 463 464 DISALLOW_COPY_AND_ASSIGN(GLRenderer); 465 }; 466 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 471 472 #if DEBUG_GL_CALLS && !defined(NDEBUG) 473 #define GLC(context, x) \ 474 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__)) 475 #else 476 #define GLC(context, x) (x) 477 #endif 478 479 } // namespace cc 480 481 #endif // CC_OUTPUT_GL_RENDERER_H_ 482