• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 blink { class WebGraphicsContext3D; }
27 
28 namespace gpu {
29 namespace gles2 {
30 class GLES2Interface;
31 }
32 }
33 
34 namespace cc {
35 
36 class GLRendererShaderTest;
37 class OutputSurface;
38 class PictureDrawQuad;
39 class ScopedResource;
40 class StreamVideoDrawQuad;
41 class TextureDrawQuad;
42 class TextureMailboxDeleter;
43 class GeometryBinding;
44 class ScopedEnsureFramebufferAllocation;
45 
46 // Class that handles drawing of composited render layers using GL.
47 class CC_EXPORT GLRenderer : public DirectRenderer {
48  public:
49   static scoped_ptr<GLRenderer> Create(
50       RendererClient* client,
51       const LayerTreeSettings* settings,
52       OutputSurface* output_surface,
53       ResourceProvider* resource_provider,
54       TextureMailboxDeleter* texture_mailbox_deleter,
55       int highp_threshold_min);
56 
57   virtual ~GLRenderer();
58 
59   virtual const RendererCapabilitiesImpl& Capabilities() const OVERRIDE;
60 
61   blink::WebGraphicsContext3D* Context();
62 
63   // Waits for rendering to finish.
64   virtual void Finish() OVERRIDE;
65 
66   virtual void DoNoOp() OVERRIDE;
67   virtual void SwapBuffers(const CompositorFrameMetadata& metadata) OVERRIDE;
68 
69   virtual void GetFramebufferPixels(void* pixels, gfx::Rect rect) OVERRIDE;
70 
71   virtual bool IsContextLost() OVERRIDE;
72 
73   virtual void SetVisible(bool visible) OVERRIDE;
74 
75   virtual void SendManagedMemoryStats(size_t bytes_visible,
76                                       size_t bytes_visible_and_nearby,
77                                       size_t bytes_allocated) OVERRIDE;
78 
79   static void DebugGLCall(gpu::gles2::GLES2Interface* gl,
80                           const char* command,
81                           const char* file,
82                           int line);
83 
84  protected:
85   GLRenderer(RendererClient* client,
86              const LayerTreeSettings* settings,
87              OutputSurface* output_surface,
88              ResourceProvider* resource_provider,
89              TextureMailboxDeleter* texture_mailbox_deleter,
90              int highp_threshold_min);
91 
IsBackbufferDiscarded()92   bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; }
93   void InitializeGrContext();
94 
SharedGeometryQuad()95   const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; }
SharedGeometry()96   const GeometryBinding* SharedGeometry() const {
97     return shared_geometry_.get();
98   }
99 
100   void GetFramebufferPixelsAsync(gfx::Rect rect,
101                                  scoped_ptr<CopyOutputRequest> request);
102   void GetFramebufferTexture(unsigned texture_id,
103                              ResourceFormat texture_format,
104                              gfx::Rect device_rect);
105   void ReleaseRenderPassTextures();
106 
107   void SetStencilEnabled(bool enabled);
stencil_enabled()108   bool stencil_enabled() const { return stencil_shadow_; }
109   void SetBlendEnabled(bool enabled);
blend_enabled()110   bool blend_enabled() const { return blend_shadow_; }
111 
112   virtual void BindFramebufferToOutputSurface(DrawingFrame* frame) OVERRIDE;
113   virtual bool BindFramebufferToTexture(DrawingFrame* frame,
114                                         const ScopedResource* resource,
115                                         gfx::Rect target_rect) OVERRIDE;
116   virtual void SetDrawViewport(gfx::Rect window_space_viewport) OVERRIDE;
117   virtual void SetScissorTestRect(gfx::Rect scissor_rect) OVERRIDE;
118   virtual void DiscardPixels(bool has_external_stencil_test,
119                              bool draw_rect_covers_full_surface) OVERRIDE;
120   virtual void ClearFramebuffer(DrawingFrame* frame,
121                                 bool has_external_stencil_test) OVERRIDE;
122   virtual void DoDrawQuad(DrawingFrame* frame, const class DrawQuad*) OVERRIDE;
123   virtual void BeginDrawingFrame(DrawingFrame* frame) OVERRIDE;
124   virtual void FinishDrawingFrame(DrawingFrame* frame) OVERRIDE;
125   virtual bool FlippedFramebuffer() const OVERRIDE;
126   virtual void EnsureScissorTestEnabled() OVERRIDE;
127   virtual void EnsureScissorTestDisabled() OVERRIDE;
128   virtual void CopyCurrentRenderPassToBitmap(
129       DrawingFrame* frame,
130       scoped_ptr<CopyOutputRequest> request) OVERRIDE;
131   virtual void FinishDrawingQuadList() OVERRIDE;
132 
133   // Check if quad needs antialiasing and if so, inflate the quad and
134   // fill edge array for fragment shader.  local_quad is set to
135   // inflated quad if antialiasing is required, otherwise it is left
136   // unchanged.  edge array is filled with inflated quad's edge data
137   // if antialiasing is required, otherwise it is left unchanged.
138   // Returns true if quad requires antialiasing and false otherwise.
139   static bool SetupQuadForAntialiasing(const gfx::Transform& device_transform,
140                                        const DrawQuad* quad,
141                                        gfx::QuadF* local_quad,
142                                        float edge[24]);
143 
144  private:
145   friend class GLRendererShaderPixelTest;
146   friend class GLRendererShaderTest;
147 
148   static void ToGLMatrix(float* gl_matrix, const gfx::Transform& transform);
149 
150   void DrawCheckerboardQuad(const DrawingFrame* frame,
151                             const CheckerboardDrawQuad* quad);
152   void DrawDebugBorderQuad(const DrawingFrame* frame,
153                            const DebugBorderDrawQuad* quad);
154   scoped_ptr<ScopedResource> GetBackgroundWithFilters(
155       DrawingFrame* frame,
156       const RenderPassDrawQuad* quad,
157       const gfx::Transform& contents_device_transform,
158       const gfx::Transform& contents_device_transformInverse,
159       bool* background_changed);
160   void DrawRenderPassQuad(DrawingFrame* frame, const RenderPassDrawQuad* quad);
161   void DrawSolidColorQuad(const DrawingFrame* frame,
162                           const SolidColorDrawQuad* quad);
163   void DrawStreamVideoQuad(const DrawingFrame* frame,
164                            const StreamVideoDrawQuad* quad);
165   void EnqueueTextureQuad(const DrawingFrame* frame,
166                           const TextureDrawQuad* quad);
167   void FlushTextureQuadCache();
168   void DrawIOSurfaceQuad(const DrawingFrame* frame,
169                          const IOSurfaceDrawQuad* quad);
170   void DrawTileQuad(const DrawingFrame* frame, const TileDrawQuad* quad);
171   void DrawContentQuad(const DrawingFrame* frame,
172                        const ContentDrawQuadBase* quad,
173                        ResourceProvider::ResourceId resource_id);
174   void DrawYUVVideoQuad(const DrawingFrame* frame,
175                         const YUVVideoDrawQuad* quad);
176   void DrawPictureQuad(const DrawingFrame* frame,
177                        const PictureDrawQuad* quad);
178 
179   void SetShaderOpacity(float opacity, int alpha_location);
180   void SetShaderQuadF(const gfx::QuadF& quad, int quad_location);
181   void DrawQuadGeometry(const DrawingFrame* frame,
182                         const gfx::Transform& draw_transform,
183                         const gfx::RectF& quad_rect,
184                         int matrix_location);
185   void SetUseProgram(unsigned program);
186 
187   void CopyTextureToFramebuffer(const DrawingFrame* frame,
188                                 int texture_id,
189                                 gfx::Rect rect,
190                                 const gfx::Transform& draw_matrix,
191                                 bool flip_vertically);
192 
193   bool UseScopedTexture(DrawingFrame* frame,
194                         const ScopedResource* resource,
195                         gfx::Rect viewport_rect);
196 
197   bool MakeContextCurrent();
198 
199   void InitializeSharedObjects();
200   void CleanupSharedObjects();
201 
202   typedef base::Callback<void(scoped_ptr<CopyOutputRequest> copy_request,
203                               bool success)>
204       AsyncGetFramebufferPixelsCleanupCallback;
205   void DoGetFramebufferPixels(
206       uint8* pixels,
207       gfx::Rect window_rect,
208       const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback);
209   void FinishedReadback(
210       const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback,
211       unsigned source_buffer,
212       unsigned query,
213       uint8_t* dest_pixels,
214       gfx::Size size);
215   void PassOnSkBitmap(scoped_ptr<SkBitmap> bitmap,
216                       scoped_ptr<SkAutoLockPixels> lock,
217                       scoped_ptr<CopyOutputRequest> request,
218                       bool success);
219 
220   void ReinitializeGLState();
221 
222   virtual void DiscardBackbuffer() OVERRIDE;
223   virtual void EnsureBackbuffer() OVERRIDE;
224   void EnforceMemoryPolicy();
225 
226   RendererCapabilitiesImpl capabilities_;
227 
228   unsigned offscreen_framebuffer_id_;
229 
230   scoped_ptr<GeometryBinding> shared_geometry_;
231   gfx::QuadF shared_geometry_quad_;
232 
233   // This block of bindings defines all of the programs used by the compositor
234   // itself.  Add any new programs here to GLRendererShaderTest.
235 
236   // Tiled layer shaders.
237   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha>
238       TileProgram;
239   typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA>
240       TileProgramAA;
241   typedef ProgramBinding<VertexShaderTileAA,
242                          FragmentShaderRGBATexClampSwizzleAlphaAA>
243       TileProgramSwizzleAA;
244   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque>
245       TileProgramOpaque;
246   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha>
247       TileProgramSwizzle;
248   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque>
249       TileProgramSwizzleOpaque;
250   typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard>
251       TileCheckerboardProgram;
252 
253   // Texture shaders.
254   typedef ProgramBinding<VertexShaderPosTexTransform,
255                          FragmentShaderRGBATexVaryingAlpha> TextureProgram;
256   typedef ProgramBinding<VertexShaderPosTexTransform,
257                          FragmentShaderRGBATexPremultiplyAlpha>
258       NonPremultipliedTextureProgram;
259   typedef ProgramBinding<VertexShaderPosTexTransform,
260                          FragmentShaderTexBackgroundVaryingAlpha>
261       TextureBackgroundProgram;
262   typedef ProgramBinding<VertexShaderPosTexTransform,
263                          FragmentShaderTexBackgroundPremultiplyAlpha>
264       NonPremultipliedTextureBackgroundProgram;
265 
266   // Render surface shaders.
267   typedef ProgramBinding<VertexShaderPosTexTransform,
268                          FragmentShaderRGBATexAlpha> RenderPassProgram;
269   typedef ProgramBinding<VertexShaderPosTexTransform,
270                          FragmentShaderRGBATexAlphaMask> RenderPassMaskProgram;
271   typedef ProgramBinding<VertexShaderQuadTexTransformAA,
272                          FragmentShaderRGBATexAlphaAA> RenderPassProgramAA;
273   typedef ProgramBinding<VertexShaderQuadTexTransformAA,
274                          FragmentShaderRGBATexAlphaMaskAA>
275       RenderPassMaskProgramAA;
276   typedef ProgramBinding<VertexShaderPosTexTransform,
277                          FragmentShaderRGBATexColorMatrixAlpha>
278       RenderPassColorMatrixProgram;
279   typedef ProgramBinding<VertexShaderQuadTexTransformAA,
280                          FragmentShaderRGBATexAlphaMaskColorMatrixAA>
281       RenderPassMaskColorMatrixProgramAA;
282   typedef ProgramBinding<VertexShaderQuadTexTransformAA,
283                          FragmentShaderRGBATexAlphaColorMatrixAA>
284       RenderPassColorMatrixProgramAA;
285   typedef ProgramBinding<VertexShaderPosTexTransform,
286                          FragmentShaderRGBATexAlphaMaskColorMatrix>
287       RenderPassMaskColorMatrixProgram;
288 
289   // Video shaders.
290   typedef ProgramBinding<VertexShaderVideoTransform, FragmentShaderRGBATex>
291       VideoStreamTextureProgram;
292   typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo>
293       VideoYUVProgram;
294   typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVAVideo>
295       VideoYUVAProgram;
296 
297   // Special purpose / effects shaders.
298   typedef ProgramBinding<VertexShaderPos, FragmentShaderColor>
299       DebugBorderProgram;
300   typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor>
301       SolidColorProgram;
302   typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA>
303       SolidColorProgramAA;
304 
305   const TileProgram* GetTileProgram(
306       TexCoordPrecision precision, SamplerType sampler);
307   const TileProgramOpaque* GetTileProgramOpaque(
308       TexCoordPrecision precision, SamplerType sampler);
309   const TileProgramAA* GetTileProgramAA(
310       TexCoordPrecision precision, SamplerType sampler);
311   const TileProgramSwizzle* GetTileProgramSwizzle(
312       TexCoordPrecision precision, SamplerType sampler);
313   const TileProgramSwizzleOpaque* GetTileProgramSwizzleOpaque(
314       TexCoordPrecision precision, SamplerType sampler);
315   const TileProgramSwizzleAA* GetTileProgramSwizzleAA(
316       TexCoordPrecision precision, SamplerType sampler);
317 
318   const TileCheckerboardProgram* GetTileCheckerboardProgram();
319 
320   const RenderPassProgram* GetRenderPassProgram(
321       TexCoordPrecision precision);
322   const RenderPassProgramAA* GetRenderPassProgramAA(
323       TexCoordPrecision precision);
324   const RenderPassMaskProgram* GetRenderPassMaskProgram(
325       TexCoordPrecision precision);
326   const RenderPassMaskProgramAA* GetRenderPassMaskProgramAA(
327       TexCoordPrecision precision);
328   const RenderPassColorMatrixProgram* GetRenderPassColorMatrixProgram(
329       TexCoordPrecision precision);
330   const RenderPassColorMatrixProgramAA* GetRenderPassColorMatrixProgramAA(
331       TexCoordPrecision precision);
332   const RenderPassMaskColorMatrixProgram* GetRenderPassMaskColorMatrixProgram(
333       TexCoordPrecision precision);
334   const RenderPassMaskColorMatrixProgramAA*
335       GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision);
336 
337   const TextureProgram* GetTextureProgram(
338       TexCoordPrecision precision);
339   const NonPremultipliedTextureProgram* GetNonPremultipliedTextureProgram(
340       TexCoordPrecision precision);
341   const TextureBackgroundProgram* GetTextureBackgroundProgram(
342       TexCoordPrecision precision);
343   const NonPremultipliedTextureBackgroundProgram*
344       GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision);
345   const TextureProgram* GetTextureIOSurfaceProgram(
346       TexCoordPrecision precision);
347 
348   const VideoYUVProgram* GetVideoYUVProgram(
349       TexCoordPrecision precision);
350   const VideoYUVAProgram* GetVideoYUVAProgram(
351       TexCoordPrecision precision);
352   const VideoStreamTextureProgram* GetVideoStreamTextureProgram(
353       TexCoordPrecision precision);
354 
355   const DebugBorderProgram* GetDebugBorderProgram();
356   const SolidColorProgram* GetSolidColorProgram();
357   const SolidColorProgramAA* GetSolidColorProgramAA();
358 
359   TileProgram tile_program_[NumTexCoordPrecisions][NumSamplerTypes];
360   TileProgramOpaque
361       tile_program_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
362   TileProgramAA tile_program_aa_[NumTexCoordPrecisions][NumSamplerTypes];
363   TileProgramSwizzle
364       tile_program_swizzle_[NumTexCoordPrecisions][NumSamplerTypes];
365   TileProgramSwizzleOpaque
366       tile_program_swizzle_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
367   TileProgramSwizzleAA
368       tile_program_swizzle_aa_[NumTexCoordPrecisions][NumSamplerTypes];
369 
370   TileCheckerboardProgram tile_checkerboard_program_;
371 
372   TextureProgram texture_program_[NumTexCoordPrecisions];
373   NonPremultipliedTextureProgram
374       nonpremultiplied_texture_program_[NumTexCoordPrecisions];
375   TextureBackgroundProgram texture_background_program_[NumTexCoordPrecisions];
376   NonPremultipliedTextureBackgroundProgram
377       nonpremultiplied_texture_background_program_[NumTexCoordPrecisions];
378   TextureProgram texture_io_surface_program_[NumTexCoordPrecisions];
379 
380   RenderPassProgram render_pass_program_[NumTexCoordPrecisions];
381   RenderPassProgramAA render_pass_program_aa_[NumTexCoordPrecisions];
382   RenderPassMaskProgram render_pass_mask_program_[NumTexCoordPrecisions];
383   RenderPassMaskProgramAA render_pass_mask_program_aa_[NumTexCoordPrecisions];
384   RenderPassColorMatrixProgram
385       render_pass_color_matrix_program_[NumTexCoordPrecisions];
386   RenderPassColorMatrixProgramAA
387       render_pass_color_matrix_program_aa_[NumTexCoordPrecisions];
388   RenderPassMaskColorMatrixProgram
389       render_pass_mask_color_matrix_program_[NumTexCoordPrecisions];
390   RenderPassMaskColorMatrixProgramAA
391       render_pass_mask_color_matrix_program_aa_[NumTexCoordPrecisions];
392 
393   VideoYUVProgram video_yuv_program_[NumTexCoordPrecisions];
394   VideoYUVAProgram video_yuva_program_[NumTexCoordPrecisions];
395   VideoStreamTextureProgram
396       video_stream_texture_program_[NumTexCoordPrecisions];
397 
398   DebugBorderProgram debug_border_program_;
399   SolidColorProgram solid_color_program_;
400   SolidColorProgramAA solid_color_program_aa_;
401 
402   blink::WebGraphicsContext3D* context_;
403   gpu::gles2::GLES2Interface* gl_;
404   gpu::ContextSupport* context_support_;
405 
406   skia::RefPtr<GrContext> gr_context_;
407   skia::RefPtr<SkCanvas> sk_canvas_;
408 
409   TextureMailboxDeleter* texture_mailbox_deleter_;
410 
411   gfx::Rect swap_buffer_rect_;
412   gfx::Rect scissor_rect_;
413   gfx::Rect viewport_;
414   bool is_backbuffer_discarded_;
415   bool is_using_bind_uniform_;
416   bool visible_;
417   bool is_scissor_enabled_;
418   bool scissor_rect_needs_reset_;
419   bool stencil_shadow_;
420   bool blend_shadow_;
421   unsigned program_shadow_;
422   TexturedQuadDrawCache draw_cache_;
423   int highp_threshold_min_;
424   int highp_threshold_cache_;
425 
426   struct PendingAsyncReadPixels;
427   ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_;
428 
429   scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_;
430 
431   scoped_refptr<ResourceProvider::Fence> last_swap_fence_;
432 
433   SkBitmap on_demand_tile_raster_bitmap_;
434   ResourceProvider::ResourceId on_demand_tile_raster_resource_id_;
435 
436   DISALLOW_COPY_AND_ASSIGN(GLRenderer);
437 };
438 
439 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
440 // call made by the compositor. Useful for debugging rendering issues but
441 // will significantly degrade performance.
442 #define DEBUG_GL_CALLS 0
443 
444 #if DEBUG_GL_CALLS && !defined(NDEBUG)
445 #define GLC(context, x)                                                        \
446   (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
447 #else
448 #define GLC(context, x) (x)
449 #endif
450 
451 }  // namespace cc
452 
453 #endif  // CC_OUTPUT_GL_RENDERER_H_
454