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