a6a9b2eb0592d8aba6cb7524ea4b49d1a04e2b37
[platform/framework/web/crosswalk.git] / src / cc / output / gl_renderer.h
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 void GetFramebufferPixels(void* pixels,
69                                     const gfx::Rect& rect) OVERRIDE;
70
71   virtual bool IsContextLost() OVERRIDE;
72
73   virtual void SendManagedMemoryStats(size_t bytes_visible,
74                                       size_t bytes_visible_and_nearby,
75                                       size_t bytes_allocated) OVERRIDE;
76
77   static void DebugGLCall(gpu::gles2::GLES2Interface* gl,
78                           const char* command,
79                           const char* file,
80                           int line);
81
82  protected:
83   GLRenderer(RendererClient* client,
84              const LayerTreeSettings* settings,
85              OutputSurface* output_surface,
86              ResourceProvider* resource_provider,
87              TextureMailboxDeleter* texture_mailbox_deleter,
88              int highp_threshold_min);
89
90   virtual void DidChangeVisibility() OVERRIDE;
91
92   bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; }
93   void InitializeGrContext();
94
95   const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; }
96   const GeometryBinding* SharedGeometry() const {
97     return shared_geometry_.get();
98   }
99
100   void GetFramebufferPixelsAsync(const gfx::Rect& rect,
101                                  scoped_ptr<CopyOutputRequest> request);
102   void GetFramebufferTexture(unsigned texture_id,
103                              ResourceFormat texture_format,
104                              const gfx::Rect& device_rect);
105   void ReleaseRenderPassTextures();
106
107   void SetStencilEnabled(bool enabled);
108   bool stencil_enabled() const { return stencil_shadow_; }
109   void SetBlendEnabled(bool 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                                         const gfx::Rect& target_rect) OVERRIDE;
116   virtual void SetDrawViewport(const gfx::Rect& window_space_viewport) OVERRIDE;
117   virtual void SetScissorTestRect(const 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                                 const gfx::Rect& rect,
190                                 const gfx::Transform& draw_matrix,
191                                 bool flip_vertically);
192
193   bool UseScopedTexture(DrawingFrame* frame,
194                         const ScopedResource* resource,
195                         const 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       const 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       const 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   void RestoreGLState();
222   void RestoreFramebuffer(DrawingFrame* frame);
223
224   virtual void DiscardBackbuffer() OVERRIDE;
225   virtual void EnsureBackbuffer() OVERRIDE;
226   void EnforceMemoryPolicy();
227
228   void ScheduleOverlays(DrawingFrame* frame);
229
230   typedef ScopedPtrVector<ResourceProvider::ScopedReadLockGL>
231       OverlayResourceLockList;
232   OverlayResourceLockList pending_overlay_resources_;
233   OverlayResourceLockList in_use_overlay_resources_;
234
235   RendererCapabilitiesImpl capabilities_;
236
237   unsigned offscreen_framebuffer_id_;
238
239   scoped_ptr<GeometryBinding> shared_geometry_;
240   gfx::QuadF shared_geometry_quad_;
241
242   // This block of bindings defines all of the programs used by the compositor
243   // itself.  Add any new programs here to GLRendererShaderTest.
244
245   // Tiled layer shaders.
246   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha>
247       TileProgram;
248   typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA>
249       TileProgramAA;
250   typedef ProgramBinding<VertexShaderTileAA,
251                          FragmentShaderRGBATexClampSwizzleAlphaAA>
252       TileProgramSwizzleAA;
253   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque>
254       TileProgramOpaque;
255   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha>
256       TileProgramSwizzle;
257   typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque>
258       TileProgramSwizzleOpaque;
259   typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard>
260       TileCheckerboardProgram;
261
262   // Texture shaders.
263   typedef ProgramBinding<VertexShaderPosTexTransform,
264                          FragmentShaderRGBATexVaryingAlpha> TextureProgram;
265   typedef ProgramBinding<VertexShaderPosTexTransform,
266                          FragmentShaderRGBATexPremultiplyAlpha>
267       NonPremultipliedTextureProgram;
268   typedef ProgramBinding<VertexShaderPosTexTransform,
269                          FragmentShaderTexBackgroundVaryingAlpha>
270       TextureBackgroundProgram;
271   typedef ProgramBinding<VertexShaderPosTexTransform,
272                          FragmentShaderTexBackgroundPremultiplyAlpha>
273       NonPremultipliedTextureBackgroundProgram;
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, FragmentShaderRGBATex>
300       VideoStreamTextureProgram;
301   typedef ProgramBinding<VertexShaderPosTexYUVStretchOffset,
302                          FragmentShaderYUVVideo> VideoYUVProgram;
303   typedef ProgramBinding<VertexShaderPosTexYUVStretchOffset,
304                          FragmentShaderYUVAVideo> VideoYUVAProgram;
305
306   // Special purpose / effects shaders.
307   typedef ProgramBinding<VertexShaderPos, FragmentShaderColor>
308       DebugBorderProgram;
309   typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor>
310       SolidColorProgram;
311   typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA>
312       SolidColorProgramAA;
313
314   const TileProgram* GetTileProgram(
315       TexCoordPrecision precision, SamplerType sampler);
316   const TileProgramOpaque* GetTileProgramOpaque(
317       TexCoordPrecision precision, SamplerType sampler);
318   const TileProgramAA* GetTileProgramAA(
319       TexCoordPrecision precision, SamplerType sampler);
320   const TileProgramSwizzle* GetTileProgramSwizzle(
321       TexCoordPrecision precision, SamplerType sampler);
322   const TileProgramSwizzleOpaque* GetTileProgramSwizzleOpaque(
323       TexCoordPrecision precision, SamplerType sampler);
324   const TileProgramSwizzleAA* GetTileProgramSwizzleAA(
325       TexCoordPrecision precision, SamplerType sampler);
326
327   const TileCheckerboardProgram* GetTileCheckerboardProgram();
328
329   const RenderPassProgram* GetRenderPassProgram(
330       TexCoordPrecision precision);
331   const RenderPassProgramAA* GetRenderPassProgramAA(
332       TexCoordPrecision precision);
333   const RenderPassMaskProgram* GetRenderPassMaskProgram(
334       TexCoordPrecision precision);
335   const RenderPassMaskProgramAA* GetRenderPassMaskProgramAA(
336       TexCoordPrecision precision);
337   const RenderPassColorMatrixProgram* GetRenderPassColorMatrixProgram(
338       TexCoordPrecision precision);
339   const RenderPassColorMatrixProgramAA* GetRenderPassColorMatrixProgramAA(
340       TexCoordPrecision precision);
341   const RenderPassMaskColorMatrixProgram* GetRenderPassMaskColorMatrixProgram(
342       TexCoordPrecision precision);
343   const RenderPassMaskColorMatrixProgramAA*
344       GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision);
345
346   const TextureProgram* GetTextureProgram(
347       TexCoordPrecision precision);
348   const NonPremultipliedTextureProgram* GetNonPremultipliedTextureProgram(
349       TexCoordPrecision precision);
350   const TextureBackgroundProgram* GetTextureBackgroundProgram(
351       TexCoordPrecision precision);
352   const NonPremultipliedTextureBackgroundProgram*
353       GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision);
354   const TextureProgram* GetTextureIOSurfaceProgram(
355       TexCoordPrecision precision);
356
357   const VideoYUVProgram* GetVideoYUVProgram(
358       TexCoordPrecision precision);
359   const VideoYUVAProgram* GetVideoYUVAProgram(
360       TexCoordPrecision precision);
361   const VideoStreamTextureProgram* GetVideoStreamTextureProgram(
362       TexCoordPrecision precision);
363
364   const DebugBorderProgram* GetDebugBorderProgram();
365   const SolidColorProgram* GetSolidColorProgram();
366   const SolidColorProgramAA* GetSolidColorProgramAA();
367
368   TileProgram tile_program_[NumTexCoordPrecisions][NumSamplerTypes];
369   TileProgramOpaque
370       tile_program_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
371   TileProgramAA tile_program_aa_[NumTexCoordPrecisions][NumSamplerTypes];
372   TileProgramSwizzle
373       tile_program_swizzle_[NumTexCoordPrecisions][NumSamplerTypes];
374   TileProgramSwizzleOpaque
375       tile_program_swizzle_opaque_[NumTexCoordPrecisions][NumSamplerTypes];
376   TileProgramSwizzleAA
377       tile_program_swizzle_aa_[NumTexCoordPrecisions][NumSamplerTypes];
378
379   TileCheckerboardProgram tile_checkerboard_program_;
380
381   TextureProgram texture_program_[NumTexCoordPrecisions];
382   NonPremultipliedTextureProgram
383       nonpremultiplied_texture_program_[NumTexCoordPrecisions];
384   TextureBackgroundProgram texture_background_program_[NumTexCoordPrecisions];
385   NonPremultipliedTextureBackgroundProgram
386       nonpremultiplied_texture_background_program_[NumTexCoordPrecisions];
387   TextureProgram texture_io_surface_program_[NumTexCoordPrecisions];
388
389   RenderPassProgram render_pass_program_[NumTexCoordPrecisions];
390   RenderPassProgramAA render_pass_program_aa_[NumTexCoordPrecisions];
391   RenderPassMaskProgram render_pass_mask_program_[NumTexCoordPrecisions];
392   RenderPassMaskProgramAA render_pass_mask_program_aa_[NumTexCoordPrecisions];
393   RenderPassColorMatrixProgram
394       render_pass_color_matrix_program_[NumTexCoordPrecisions];
395   RenderPassColorMatrixProgramAA
396       render_pass_color_matrix_program_aa_[NumTexCoordPrecisions];
397   RenderPassMaskColorMatrixProgram
398       render_pass_mask_color_matrix_program_[NumTexCoordPrecisions];
399   RenderPassMaskColorMatrixProgramAA
400       render_pass_mask_color_matrix_program_aa_[NumTexCoordPrecisions];
401
402   VideoYUVProgram video_yuv_program_[NumTexCoordPrecisions];
403   VideoYUVAProgram video_yuva_program_[NumTexCoordPrecisions];
404   VideoStreamTextureProgram
405       video_stream_texture_program_[NumTexCoordPrecisions];
406
407   DebugBorderProgram debug_border_program_;
408   SolidColorProgram solid_color_program_;
409   SolidColorProgramAA solid_color_program_aa_;
410
411   gpu::gles2::GLES2Interface* gl_;
412   gpu::ContextSupport* context_support_;
413
414   skia::RefPtr<GrContext> gr_context_;
415   skia::RefPtr<SkCanvas> sk_canvas_;
416
417   TextureMailboxDeleter* texture_mailbox_deleter_;
418
419   gfx::Rect swap_buffer_rect_;
420   gfx::Rect scissor_rect_;
421   gfx::Rect viewport_;
422   bool is_backbuffer_discarded_;
423   bool is_using_bind_uniform_;
424   bool is_scissor_enabled_;
425   bool scissor_rect_needs_reset_;
426   bool stencil_shadow_;
427   bool blend_shadow_;
428   unsigned program_shadow_;
429   TexturedQuadDrawCache draw_cache_;
430   int highp_threshold_min_;
431   int highp_threshold_cache_;
432
433   struct PendingAsyncReadPixels;
434   ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_;
435
436   scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_;
437
438   class SyncQuery;
439   ScopedPtrDeque<SyncQuery> pending_sync_queries_;
440   ScopedPtrDeque<SyncQuery> available_sync_queries_;
441   scoped_ptr<SyncQuery> current_sync_query_;
442   bool use_sync_query_;
443
444   SkBitmap on_demand_tile_raster_bitmap_;
445   ResourceProvider::ResourceId on_demand_tile_raster_resource_id_;
446
447   DISALLOW_COPY_AND_ASSIGN(GLRenderer);
448 };
449
450 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
451 // call made by the compositor. Useful for debugging rendering issues but
452 // will significantly degrade performance.
453 #define DEBUG_GL_CALLS 0
454
455 #if DEBUG_GL_CALLS && !defined(NDEBUG)
456 #define GLC(context, x)                                                        \
457   (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
458 #else
459 #define GLC(context, x) (x)
460 #endif
461
462 }  // namespace cc
463
464 #endif  // CC_OUTPUT_GL_RENDERER_H_