Home | History | Annotate | Download | only in output
      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_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/quad_f.h"
     24 
     25 class SkBitmap;
     26 
     27 namespace gpu {
     28 namespace gles2 {
     29 class GLES2Interface;
     30 }
     31 }
     32 
     33 namespace cc {
     34 
     35 class GLRendererShaderTest;
     36 class OutputSurface;
     37 class PictureDrawQuad;
     38 class ScopedResource;
     39 class StreamVideoDrawQuad;
     40 class TextureDrawQuad;
     41 class TextureMailboxDeleter;
     42 class GeometryBinding;
     43 class ScopedEnsureFramebufferAllocation;
     44 
     45 // Class that handles drawing of composited render layers using GL.
     46 class CC_EXPORT GLRenderer : public DirectRenderer {
     47  public:
     48   class ScopedUseGrContext;
     49 
     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);
     57 
     58   virtual ~GLRenderer();
     59 
     60   virtual const RendererCapabilitiesImpl& Capabilities() const OVERRIDE;
     61 
     62   // Waits for rendering to finish.
     63   virtual void Finish() OVERRIDE;
     64 
     65   virtual void DoNoOp() OVERRIDE;
     66   virtual void SwapBuffers(const CompositorFrameMetadata& metadata) OVERRIDE;
     67 
     68   virtual bool IsContextLost() OVERRIDE;
     69 
     70   static void DebugGLCall(gpu::gles2::GLES2Interface* gl,
     71                           const char* command,
     72                           const char* file,
     73                           int line);
     74 
     75  protected:
     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);
     82 
     83   virtual void DidChangeVisibility() OVERRIDE;
     84 
     85   bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; }
     86 
     87   const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; }
     88   const GeometryBinding* SharedGeometry() const {
     89     return shared_geometry_.get();
     90   }
     91 
     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();
     98 
     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_; }
    103 
    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(
    121       DrawingFrame* frame,
    122       scoped_ptr<CopyOutputRequest> request) OVERRIDE;
    123   virtual void FinishDrawingQuadList() OVERRIDE;
    124 
    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,
    134                                        float edge[24]);
    135 
    136  private:
    137   friend class GLRendererShaderPixelTest;
    138   friend class GLRendererShaderTest;
    139 
    140   static void ToGLMatrix(float* gl_matrix, const gfx::Transform& transform);
    141 
    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(
    147       DrawingFrame* frame,
    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);
    170 
    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);
    178 
    179   void CopyTextureToFramebuffer(const DrawingFrame* frame,
    180                                 int texture_id,
    181                                 const gfx::Rect& rect,
    182                                 const gfx::Transform& draw_matrix,
    183                                 bool flip_vertically);
    184 
    185   bool UseScopedTexture(DrawingFrame* frame,
    186                         const ScopedResource* resource,
    187                         const gfx::Rect& viewport_rect);
    188 
    189   bool MakeContextCurrent();
    190 
    191   void InitializeSharedObjects();
    192   void CleanupSharedObjects();
    193 
    194   typedef base::Callback<void(scoped_ptr<CopyOutputRequest> copy_request,
    195                               bool success)>
    196       AsyncGetFramebufferPixelsCleanupCallback;
    197   void FinishedReadback(unsigned source_buffer,
    198                         unsigned query,
    199                         const gfx::Size& size);
    200 
    201   void ReinitializeGLState();
    202   void RestoreGLState();
    203   void RestoreFramebuffer(DrawingFrame* frame);
    204 
    205   virtual void DiscardBackbuffer() OVERRIDE;
    206   virtual void EnsureBackbuffer() OVERRIDE;
    207   void EnforceMemoryPolicy();
    208 
    209   void ScheduleOverlays(DrawingFrame* frame);
    210 
    211   typedef ScopedPtrVector<ResourceProvider::ScopedReadLockGL>
    212       OverlayResourceLockList;
    213   OverlayResourceLockList pending_overlay_resources_;
    214   OverlayResourceLockList in_use_overlay_resources_;
    215 
    216   RendererCapabilitiesImpl capabilities_;
    217 
    218   unsigned offscreen_framebuffer_id_;
    219 
    220   scoped_ptr<GeometryBinding> shared_geometry_;
    221   gfx::QuadF shared_geometry_quad_;
    222 
    223   // This block of bindings defines all of the programs used by the compositor
    224   // itself.  Add any new programs here to GLRendererShaderTest.
    225 
    226   // Tiled layer shaders.
    227   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha>
    228       TileProgram;
    229   typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA>
    230       TileProgramAA;
    231   typedef ProgramBinding<VertexShaderTileAA,
    232                          FragmentShaderRGBATexClampSwizzleAlphaAA>
    233       TileProgramSwizzleAA;
    234   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque>
    235       TileProgramOpaque;
    236   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha>
    237       TileProgramSwizzle;
    238   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque>
    239       TileProgramSwizzleOpaque;
    240   typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard>
    241       TileCheckerboardProgram;
    242 
    243   // Texture shaders.
    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;
    255 
    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;
    278 
    279   // Video shaders.
    280   typedef ProgramBinding<VertexShaderVideoTransform, FragmentShaderRGBATex>
    281       VideoStreamTextureProgram;
    282   typedef ProgramBinding<VertexShaderPosTexYUVStretchOffset,
    283                          FragmentShaderYUVVideo> VideoYUVProgram;
    284   typedef ProgramBinding<VertexShaderPosTexYUVStretchOffset,
    285                          FragmentShaderYUVAVideo> VideoYUVAProgram;
    286 
    287   // Special purpose / effects shaders.
    288   typedef ProgramBinding<VertexShaderPos, FragmentShaderColor>
    289       DebugBorderProgram;
    290   typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor>
    291       SolidColorProgram;
    292   typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA>
    293       SolidColorProgramAA;
    294 
    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);
    307 
    308   const TileCheckerboardProgram* GetTileCheckerboardProgram();
    309 
    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);
    326 
    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);
    337 
    338   const VideoYUVProgram* GetVideoYUVProgram(
    339       TexCoordPrecision precision);
    340   const VideoYUVAProgram* GetVideoYUVAProgram(
    341       TexCoordPrecision precision);
    342   const VideoStreamTextureProgram* GetVideoStreamTextureProgram(
    343       TexCoordPrecision precision);
    344 
    345   const DebugBorderProgram* GetDebugBorderProgram();
    346   const SolidColorProgram* GetSolidColorProgram();
    347   const SolidColorProgramAA* GetSolidColorProgramAA();
    348 
    349   TileProgram tile_program_[NumTexCoordPrecisions][NumSamplerTypes];
    350   TileProgramOpaque
    351       tile_program_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
    352   TileProgramAA tile_program_aa_[NumTexCoordPrecisions][NumSamplerTypes];
    353   TileProgramSwizzle
    354       tile_program_swizzle_[NumTexCoordPrecisions][NumSamplerTypes];
    355   TileProgramSwizzleOpaque
    356       tile_program_swizzle_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
    357   TileProgramSwizzleAA
    358       tile_program_swizzle_aa_[NumTexCoordPrecisions][NumSamplerTypes];
    359 
    360   TileCheckerboardProgram tile_checkerboard_program_;
    361 
    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];
    369 
    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];
    382 
    383   VideoYUVProgram video_yuv_program_[NumTexCoordPrecisions];
    384   VideoYUVAProgram video_yuva_program_[NumTexCoordPrecisions];
    385   VideoStreamTextureProgram
    386       video_stream_texture_program_[NumTexCoordPrecisions];
    387 
    388   DebugBorderProgram debug_border_program_;
    389   SolidColorProgram solid_color_program_;
    390   SolidColorProgramAA solid_color_program_aa_;
    391 
    392   gpu::gles2::GLES2Interface* gl_;
    393   gpu::ContextSupport* context_support_;
    394 
    395   skia::RefPtr<GrContext> gr_context_;
    396   skia::RefPtr<SkCanvas> sk_canvas_;
    397 
    398   TextureMailboxDeleter* texture_mailbox_deleter_;
    399 
    400   gfx::Rect swap_buffer_rect_;
    401   gfx::Rect scissor_rect_;
    402   gfx::Rect viewport_;
    403   bool is_backbuffer_discarded_;
    404   bool is_using_bind_uniform_;
    405   bool is_scissor_enabled_;
    406   bool scissor_rect_needs_reset_;
    407   bool stencil_shadow_;
    408   bool blend_shadow_;
    409   unsigned program_shadow_;
    410   TexturedQuadDrawCache draw_cache_;
    411   int highp_threshold_min_;
    412   int highp_threshold_cache_;
    413 
    414   struct PendingAsyncReadPixels;
    415   ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_;
    416 
    417   scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_;
    418 
    419   class SyncQuery;
    420   ScopedPtrDeque<SyncQuery> pending_sync_queries_;
    421   ScopedPtrDeque<SyncQuery> available_sync_queries_;
    422   scoped_ptr<SyncQuery> current_sync_query_;
    423   bool use_sync_query_;
    424 
    425   SkBitmap on_demand_tile_raster_bitmap_;
    426   ResourceProvider::ResourceId on_demand_tile_raster_resource_id_;
    427 
    428   DISALLOW_COPY_AND_ASSIGN(GLRenderer);
    429 };
    430 
    431 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
    432 // call made by the compositor. Useful for debugging rendering issues but
    433 // will significantly degrade performance.
    434 #define DEBUG_GL_CALLS 0
    435 
    436 #if DEBUG_GL_CALLS && !defined(NDEBUG)
    437 #define GLC(context, x)                                                        \
    438   (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
    439 #else
    440 #define GLC(context, x) (x)
    441 #endif
    442 
    443 }  // namespace cc
    444 
    445 #endif  // CC_OUTPUT_GL_RENDERER_H_
    446