Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / cc / output / gl_renderer.h
blob13f65f2cc5c55395c557fbb051284ffe3c5e1f5d
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.
5 #ifndef CC_OUTPUT_GL_RENDERER_H_
6 #define CC_OUTPUT_GL_RENDERER_H_
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"
24 class SkBitmap;
26 namespace WebKit { class WebGraphicsContext3D; }
28 namespace cc {
30 class GLRendererShaderTest;
31 class OutputSurface;
32 class PictureDrawQuad;
33 class ScopedResource;
34 class StreamVideoDrawQuad;
35 class TextureDrawQuad;
36 class TextureMailboxDeleter;
37 class GeometryBinding;
38 class ScopedEnsureFramebufferAllocation;
40 // Class that handles drawing of composited render layers using GL.
41 class CC_EXPORT GLRenderer : public DirectRenderer {
42 public:
43 static scoped_ptr<GLRenderer> Create(
44 RendererClient* client,
45 const LayerTreeSettings* settings,
46 OutputSurface* output_surface,
47 ResourceProvider* resource_provider,
48 TextureMailboxDeleter* texture_mailbox_deleter,
49 int highp_threshold_min,
50 bool use_skia_gpu_backend);
52 virtual ~GLRenderer();
54 virtual const RendererCapabilities& Capabilities() const OVERRIDE;
56 WebKit::WebGraphicsContext3D* Context();
58 virtual void ViewportChanged() OVERRIDE;
60 // Waits for rendering to finish.
61 virtual void Finish() OVERRIDE;
63 virtual void DoNoOp() OVERRIDE;
64 virtual void SwapBuffers() OVERRIDE;
66 virtual void GetFramebufferPixels(void* pixels, gfx::Rect rect) OVERRIDE;
68 virtual bool IsContextLost() OVERRIDE;
70 virtual void SetVisible(bool visible) OVERRIDE;
72 virtual void SendManagedMemoryStats(size_t bytes_visible,
73 size_t bytes_visible_and_nearby,
74 size_t bytes_allocated) OVERRIDE;
76 virtual void SetDiscardBackBufferWhenNotVisible(bool discard) OVERRIDE;
78 static void DebugGLCall(WebKit::WebGraphicsContext3D* context,
79 const char* command,
80 const char* file,
81 int line);
83 bool CanUseSkiaGPUBackend() const;
85 protected:
86 GLRenderer(RendererClient* client,
87 const LayerTreeSettings* settings,
88 OutputSurface* output_surface,
89 ResourceProvider* resource_provider,
90 TextureMailboxDeleter* texture_mailbox_deleter,
91 int highp_threshold_min);
93 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; }
94 bool Initialize();
95 void InitializeGrContext();
97 const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; }
98 const GeometryBinding* SharedGeometry() const {
99 return shared_geometry_.get();
102 void GetFramebufferPixelsAsync(gfx::Rect rect,
103 scoped_ptr<CopyOutputRequest> request);
104 void GetFramebufferTexture(unsigned texture_id,
105 ResourceFormat texture_format,
106 gfx::Rect device_rect);
107 void ReleaseRenderPassTextures();
109 void SetStencilEnabled(bool enabled);
110 bool stencil_enabled() const { return stencil_shadow_; }
111 void SetBlendEnabled(bool enabled);
112 bool blend_enabled() const { return blend_shadow_; }
114 virtual void BindFramebufferToOutputSurface(DrawingFrame* frame) OVERRIDE;
115 virtual bool BindFramebufferToTexture(DrawingFrame* frame,
116 const ScopedResource* resource,
117 gfx::Rect target_rect) OVERRIDE;
118 virtual void SetDrawViewport(gfx::Rect window_space_viewport) OVERRIDE;
119 virtual void SetScissorTestRect(gfx::Rect scissor_rect) OVERRIDE;
120 virtual void ClearFramebuffer(DrawingFrame* frame) OVERRIDE;
121 virtual void DoDrawQuad(DrawingFrame* frame, const class DrawQuad*) OVERRIDE;
122 virtual void BeginDrawingFrame(DrawingFrame* frame) OVERRIDE;
123 virtual void FinishDrawingFrame(DrawingFrame* frame) OVERRIDE;
124 virtual bool FlippedFramebuffer() const OVERRIDE;
125 virtual void EnsureScissorTestEnabled() OVERRIDE;
126 virtual void EnsureScissorTestDisabled() OVERRIDE;
127 virtual void CopyCurrentRenderPassToBitmap(
128 DrawingFrame* frame,
129 scoped_ptr<CopyOutputRequest> request) OVERRIDE;
130 virtual void FinishDrawingQuadList() OVERRIDE;
132 // Check if quad needs antialiasing and if so, inflate the quad and
133 // fill edge array for fragment shader. local_quad is set to
134 // inflated quad if antialiasing is required, otherwise it is left
135 // unchanged. edge array is filled with inflated quad's edge data
136 // if antialiasing is required, otherwise it is left unchanged.
137 // Returns true if quad requires antialiasing and false otherwise.
138 static bool SetupQuadForAntialiasing(const gfx::Transform& device_transform,
139 const DrawQuad* quad,
140 gfx::QuadF* local_quad,
141 float edge[24]);
143 private:
144 friend class GLRendererShaderPixelTest;
145 friend class GLRendererShaderTest;
147 static void ToGLMatrix(float* gl_matrix, const gfx::Transform& transform);
149 void DrawCheckerboardQuad(const DrawingFrame* frame,
150 const CheckerboardDrawQuad* quad);
151 void DrawDebugBorderQuad(const DrawingFrame* frame,
152 const DebugBorderDrawQuad* quad);
153 scoped_ptr<ScopedResource> DrawBackgroundFilters(
154 DrawingFrame* frame,
155 const RenderPassDrawQuad* quad,
156 const gfx::Transform& contents_device_transform,
157 const gfx::Transform& contents_device_transformInverse);
158 void DrawRenderPassQuad(DrawingFrame* frame, const RenderPassDrawQuad* quad);
159 void DrawSolidColorQuad(const DrawingFrame* frame,
160 const SolidColorDrawQuad* quad);
161 void DrawStreamVideoQuad(const DrawingFrame* frame,
162 const StreamVideoDrawQuad* quad);
163 void EnqueueTextureQuad(const DrawingFrame* frame,
164 const TextureDrawQuad* quad);
165 void FlushTextureQuadCache();
166 void DrawIOSurfaceQuad(const DrawingFrame* frame,
167 const IOSurfaceDrawQuad* quad);
168 void DrawTileQuad(const DrawingFrame* frame, const TileDrawQuad* quad);
169 void DrawContentQuad(const DrawingFrame* frame,
170 const ContentDrawQuadBase* quad,
171 ResourceProvider::ResourceId resource_id);
172 void DrawYUVVideoQuad(const DrawingFrame* frame,
173 const YUVVideoDrawQuad* quad);
174 void DrawPictureQuad(const DrawingFrame* frame,
175 const PictureDrawQuad* quad);
176 void DrawPictureQuadDirectToBackbuffer(const DrawingFrame* frame,
177 const PictureDrawQuad* quad);
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);
187 void CopyTextureToFramebuffer(const DrawingFrame* frame,
188 int texture_id,
189 gfx::Rect rect,
190 const gfx::Transform& draw_matrix,
191 bool flip_vertically);
193 bool UseScopedTexture(DrawingFrame* frame,
194 const ScopedResource* resource,
195 gfx::Rect viewport_rect);
197 bool MakeContextCurrent();
199 bool InitializeSharedObjects();
200 void CleanupSharedObjects();
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);
220 void ReinitializeGrCanvas();
221 void ReinitializeGLState();
223 virtual void DiscardBackbuffer() OVERRIDE;
224 virtual void EnsureBackbuffer() OVERRIDE;
225 void EnforceMemoryPolicy();
227 RendererCapabilities capabilities_;
229 unsigned offscreen_framebuffer_id_;
231 scoped_ptr<GeometryBinding> shared_geometry_;
232 gfx::QuadF shared_geometry_quad_;
234 // This block of bindings defines all of the programs used by the compositor
235 // itself. Add any new programs here to GLRendererShaderTest.
237 // Tiled layer shaders.
238 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha>
239 TileProgram;
240 typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA>
241 TileProgramAA;
242 typedef ProgramBinding<VertexShaderTileAA,
243 FragmentShaderRGBATexClampSwizzleAlphaAA>
244 TileProgramSwizzleAA;
245 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque>
246 TileProgramOpaque;
247 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha>
248 TileProgramSwizzle;
249 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque>
250 TileProgramSwizzleOpaque;
251 typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard>
252 TileCheckerboardProgram;
254 // Texture shaders.
255 typedef ProgramBinding<VertexShaderPosTexTransform,
256 FragmentShaderRGBATexVaryingAlpha> TextureProgram;
257 typedef ProgramBinding<VertexShaderPosTexTransform,
258 FragmentShaderRGBATexPremultiplyAlpha>
259 NonPremultipliedTextureProgram;
260 typedef ProgramBinding<VertexShaderPosTexTransform,
261 FragmentShaderTexBackgroundVaryingAlpha>
262 TextureBackgroundProgram;
263 typedef ProgramBinding<VertexShaderPosTexTransform,
264 FragmentShaderTexBackgroundPremultiplyAlpha>
265 NonPremultipliedTextureBackgroundProgram;
266 typedef ProgramBinding<VertexShaderPosTexTransform,
267 FragmentShaderRGBATexRectVaryingAlpha>
268 TextureIOSurfaceProgram;
270 // Render surface shaders.
271 typedef ProgramBinding<VertexShaderPosTexTransform,
272 FragmentShaderRGBATexAlpha> RenderPassProgram;
273 typedef ProgramBinding<VertexShaderPosTexTransform,
274 FragmentShaderRGBATexAlphaMask> RenderPassMaskProgram;
275 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
276 FragmentShaderRGBATexAlphaAA> RenderPassProgramAA;
277 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
278 FragmentShaderRGBATexAlphaMaskAA>
279 RenderPassMaskProgramAA;
280 typedef ProgramBinding<VertexShaderPosTexTransform,
281 FragmentShaderRGBATexColorMatrixAlpha>
282 RenderPassColorMatrixProgram;
283 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
284 FragmentShaderRGBATexAlphaMaskColorMatrixAA>
285 RenderPassMaskColorMatrixProgramAA;
286 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
287 FragmentShaderRGBATexAlphaColorMatrixAA>
288 RenderPassColorMatrixProgramAA;
289 typedef ProgramBinding<VertexShaderPosTexTransform,
290 FragmentShaderRGBATexAlphaMaskColorMatrix>
291 RenderPassMaskColorMatrixProgram;
293 // Video shaders.
294 typedef ProgramBinding<VertexShaderVideoTransform,
295 FragmentShaderOESImageExternal>
296 VideoStreamTextureProgram;
297 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo>
298 VideoYUVProgram;
299 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVAVideo>
300 VideoYUVAProgram;
302 // Special purpose / effects shaders.
303 typedef ProgramBinding<VertexShaderPos, FragmentShaderColor>
304 DebugBorderProgram;
305 typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor>
306 SolidColorProgram;
307 typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA>
308 SolidColorProgramAA;
310 const TileProgram* GetTileProgram(TexCoordPrecision precision);
311 const TileProgramOpaque* GetTileProgramOpaque(TexCoordPrecision precision);
312 const TileProgramAA* GetTileProgramAA(TexCoordPrecision precision);
313 const TileProgramSwizzle* GetTileProgramSwizzle(TexCoordPrecision precision);
314 const TileProgramSwizzleOpaque* GetTileProgramSwizzleOpaque(
315 TexCoordPrecision precision);
316 const TileProgramSwizzleAA* GetTileProgramSwizzleAA(
317 TexCoordPrecision precision);
318 const TileCheckerboardProgram* GetTileCheckerboardProgram();
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);
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 TextureIOSurfaceProgram* GetTextureIOSurfaceProgram(
346 TexCoordPrecision precision);
348 const VideoYUVProgram* GetVideoYUVProgram(
349 TexCoordPrecision precision);
350 const VideoYUVAProgram* GetVideoYUVAProgram(
351 TexCoordPrecision precision);
352 const VideoStreamTextureProgram* GetVideoStreamTextureProgram(
353 TexCoordPrecision precision);
355 const DebugBorderProgram* GetDebugBorderProgram();
356 const SolidColorProgram* GetSolidColorProgram();
357 const SolidColorProgramAA* GetSolidColorProgramAA();
359 scoped_ptr<TileProgram> tile_program_;
360 scoped_ptr<TileProgramOpaque> tile_program_opaque_;
361 scoped_ptr<TileProgramAA> tile_program_aa_;
362 scoped_ptr<TileProgramSwizzle> tile_program_swizzle_;
363 scoped_ptr<TileProgramSwizzleOpaque> tile_program_swizzle_opaque_;
364 scoped_ptr<TileProgramSwizzleAA> tile_program_swizzle_aa_;
365 scoped_ptr<TileCheckerboardProgram> tile_checkerboard_program_;
367 scoped_ptr<TileProgram> tile_program_highp_;
368 scoped_ptr<TileProgramOpaque> tile_program_opaque_highp_;
369 scoped_ptr<TileProgramAA> tile_program_aa_highp_;
370 scoped_ptr<TileProgramSwizzle> tile_program_swizzle_highp_;
371 scoped_ptr<TileProgramSwizzleOpaque> tile_program_swizzle_opaque_highp_;
372 scoped_ptr<TileProgramSwizzleAA> tile_program_swizzle_aa_highp_;
374 scoped_ptr<TextureProgram> texture_program_;
375 scoped_ptr<NonPremultipliedTextureProgram> nonpremultiplied_texture_program_;
376 scoped_ptr<TextureBackgroundProgram> texture_background_program_;
377 scoped_ptr<NonPremultipliedTextureBackgroundProgram>
378 nonpremultiplied_texture_background_program_;
379 scoped_ptr<TextureIOSurfaceProgram> texture_io_surface_program_;
381 scoped_ptr<TextureProgram> texture_program_highp_;
382 scoped_ptr<NonPremultipliedTextureProgram>
383 nonpremultiplied_texture_program_highp_;
384 scoped_ptr<TextureBackgroundProgram> texture_background_program_highp_;
385 scoped_ptr<NonPremultipliedTextureBackgroundProgram>
386 nonpremultiplied_texture_background_program_highp_;
387 scoped_ptr<TextureIOSurfaceProgram> texture_io_surface_program_highp_;
389 scoped_ptr<RenderPassProgram> render_pass_program_;
390 scoped_ptr<RenderPassProgramAA> render_pass_program_aa_;
391 scoped_ptr<RenderPassMaskProgram> render_pass_mask_program_;
392 scoped_ptr<RenderPassMaskProgramAA> render_pass_mask_program_aa_;
393 scoped_ptr<RenderPassColorMatrixProgram> render_pass_color_matrix_program_;
394 scoped_ptr<RenderPassColorMatrixProgramAA>
395 render_pass_color_matrix_program_aa_;
396 scoped_ptr<RenderPassMaskColorMatrixProgram>
397 render_pass_mask_color_matrix_program_;
398 scoped_ptr<RenderPassMaskColorMatrixProgramAA>
399 render_pass_mask_color_matrix_program_aa_;
401 scoped_ptr<RenderPassProgram> render_pass_program_highp_;
402 scoped_ptr<RenderPassProgramAA> render_pass_program_aa_highp_;
403 scoped_ptr<RenderPassMaskProgram> render_pass_mask_program_highp_;
404 scoped_ptr<RenderPassMaskProgramAA> render_pass_mask_program_aa_highp_;
405 scoped_ptr<RenderPassColorMatrixProgram>
406 render_pass_color_matrix_program_highp_;
407 scoped_ptr<RenderPassColorMatrixProgramAA>
408 render_pass_color_matrix_program_aa_highp_;
409 scoped_ptr<RenderPassMaskColorMatrixProgram>
410 render_pass_mask_color_matrix_program_highp_;
411 scoped_ptr<RenderPassMaskColorMatrixProgramAA>
412 render_pass_mask_color_matrix_program_aa_highp_;
414 scoped_ptr<VideoYUVProgram> video_yuv_program_;
415 scoped_ptr<VideoYUVAProgram> video_yuva_program_;
416 scoped_ptr<VideoStreamTextureProgram> video_stream_texture_program_;
418 scoped_ptr<VideoYUVProgram> video_yuv_program_highp_;
419 scoped_ptr<VideoYUVAProgram> video_yuva_program_highp_;
420 scoped_ptr<VideoStreamTextureProgram> video_stream_texture_program_highp_;
422 scoped_ptr<DebugBorderProgram> debug_border_program_;
423 scoped_ptr<SolidColorProgram> solid_color_program_;
424 scoped_ptr<SolidColorProgramAA> solid_color_program_aa_;
426 WebKit::WebGraphicsContext3D* context_;
428 skia::RefPtr<GrContext> gr_context_;
429 skia::RefPtr<SkCanvas> sk_canvas_;
431 TextureMailboxDeleter* texture_mailbox_deleter_;
433 gfx::Rect swap_buffer_rect_;
434 gfx::Rect scissor_rect_;
435 gfx::Rect viewport_;
436 bool is_backbuffer_discarded_;
437 bool discard_backbuffer_when_not_visible_;
438 bool is_using_bind_uniform_;
439 bool visible_;
440 bool is_scissor_enabled_;
441 bool stencil_shadow_;
442 bool blend_shadow_;
443 unsigned program_shadow_;
444 TexturedQuadDrawCache draw_cache_;
445 int highp_threshold_min_;
446 int highp_threshold_cache_;
448 struct PendingAsyncReadPixels;
449 ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_;
451 scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_;
453 scoped_refptr<ResourceProvider::Fence> last_swap_fence_;
455 SkBitmap on_demand_tile_raster_bitmap_;
456 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_;
458 DISALLOW_COPY_AND_ASSIGN(GLRenderer);
461 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
462 // call made by the compositor. Useful for debugging rendering issues but
463 // will significantly degrade performance.
464 #define DEBUG_GL_CALLS 0
466 #if DEBUG_GL_CALLS && !defined(NDEBUG)
467 #define GLC(context, x) \
468 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
469 #else
470 #define GLC(context, x) (x)
471 #endif
473 } // namespace cc
475 #endif // CC_OUTPUT_GL_RENDERER_H_