chrome.windows.create should return new window id in Guest mode
[chromium-blink-merge.git] / cc / output / gl_renderer.h
blobd65e6381723cd1b86a146a5ef6078d95f7fa6d3b
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 static void DebugGLCall(WebKit::WebGraphicsContext3D* context,
77 const char* command,
78 const char* file,
79 int line);
81 bool CanUseSkiaGPUBackend() const;
83 protected:
84 GLRenderer(RendererClient* client,
85 const LayerTreeSettings* settings,
86 OutputSurface* output_surface,
87 ResourceProvider* resource_provider,
88 TextureMailboxDeleter* texture_mailbox_deleter,
89 int highp_threshold_min);
91 bool IsBackbufferDiscarded() const { return is_backbuffer_discarded_; }
92 bool Initialize();
93 void InitializeGrContext();
95 const gfx::QuadF& SharedGeometryQuad() const { return shared_geometry_quad_; }
96 const GeometryBinding* SharedGeometry() const {
97 return shared_geometry_.get();
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();
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_; }
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;
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]);
144 private:
145 friend class GLRendererShaderPixelTest;
146 friend class GLRendererShaderTest;
148 static void ToGLMatrix(float* gl_matrix, const gfx::Transform& transform);
150 void DrawCheckerboardQuad(const DrawingFrame* frame,
151 const CheckerboardDrawQuad* quad);
152 void DrawDebugBorderQuad(const DrawingFrame* frame,
153 const DebugBorderDrawQuad* quad);
154 scoped_ptr<ScopedResource> DrawBackgroundFilters(
155 DrawingFrame* frame,
156 const RenderPassDrawQuad* quad,
157 const gfx::Transform& contents_device_transform,
158 const gfx::Transform& contents_device_transformInverse);
159 void DrawRenderPassQuad(DrawingFrame* frame, const RenderPassDrawQuad* quad);
160 void DrawSolidColorQuad(const DrawingFrame* frame,
161 const SolidColorDrawQuad* quad);
162 void DrawStreamVideoQuad(const DrawingFrame* frame,
163 const StreamVideoDrawQuad* quad);
164 void EnqueueTextureQuad(const DrawingFrame* frame,
165 const TextureDrawQuad* quad);
166 void FlushTextureQuadCache();
167 void DrawIOSurfaceQuad(const DrawingFrame* frame,
168 const IOSurfaceDrawQuad* quad);
169 void DrawTileQuad(const DrawingFrame* frame, const TileDrawQuad* quad);
170 void DrawContentQuad(const DrawingFrame* frame,
171 const ContentDrawQuadBase* quad,
172 ResourceProvider::ResourceId resource_id);
173 void DrawYUVVideoQuad(const DrawingFrame* frame,
174 const YUVVideoDrawQuad* quad);
175 void DrawPictureQuad(const DrawingFrame* frame,
176 const PictureDrawQuad* quad);
177 void DrawPictureQuadDirectToBackbuffer(const DrawingFrame* frame,
178 const PictureDrawQuad* quad);
180 void SetShaderOpacity(float opacity, int alpha_location);
181 void SetShaderQuadF(const gfx::QuadF& quad, int quad_location);
182 void DrawQuadGeometry(const DrawingFrame* frame,
183 const gfx::Transform& draw_transform,
184 const gfx::RectF& quad_rect,
185 int matrix_location);
186 void SetUseProgram(unsigned program);
188 void CopyTextureToFramebuffer(const DrawingFrame* frame,
189 int texture_id,
190 gfx::Rect rect,
191 const gfx::Transform& draw_matrix,
192 bool flip_vertically);
194 bool UseScopedTexture(DrawingFrame* frame,
195 const ScopedResource* resource,
196 gfx::Rect viewport_rect);
198 bool MakeContextCurrent();
200 bool InitializeSharedObjects();
201 void CleanupSharedObjects();
203 typedef base::Callback<void(scoped_ptr<CopyOutputRequest> copy_request,
204 bool success)>
205 AsyncGetFramebufferPixelsCleanupCallback;
206 void DoGetFramebufferPixels(
207 uint8* pixels,
208 gfx::Rect window_rect,
209 const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback);
210 void FinishedReadback(
211 const AsyncGetFramebufferPixelsCleanupCallback& cleanup_callback,
212 unsigned source_buffer,
213 unsigned query,
214 uint8_t* dest_pixels,
215 gfx::Size size);
216 void PassOnSkBitmap(scoped_ptr<SkBitmap> bitmap,
217 scoped_ptr<SkAutoLockPixels> lock,
218 scoped_ptr<CopyOutputRequest> request,
219 bool success);
221 void ReinitializeGrCanvas();
222 void ReinitializeGLState();
224 virtual void DiscardBackbuffer() OVERRIDE;
225 virtual void EnsureBackbuffer() OVERRIDE;
226 void EnforceMemoryPolicy();
228 RendererCapabilities capabilities_;
230 unsigned offscreen_framebuffer_id_;
232 scoped_ptr<GeometryBinding> shared_geometry_;
233 gfx::QuadF shared_geometry_quad_;
235 // This block of bindings defines all of the programs used by the compositor
236 // itself. Add any new programs here to GLRendererShaderTest.
238 // Tiled layer shaders.
239 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexAlpha>
240 TileProgram;
241 typedef ProgramBinding<VertexShaderTileAA, FragmentShaderRGBATexClampAlphaAA>
242 TileProgramAA;
243 typedef ProgramBinding<VertexShaderTileAA,
244 FragmentShaderRGBATexClampSwizzleAlphaAA>
245 TileProgramSwizzleAA;
246 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexOpaque>
247 TileProgramOpaque;
248 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleAlpha>
249 TileProgramSwizzle;
250 typedef ProgramBinding<VertexShaderTile, FragmentShaderRGBATexSwizzleOpaque>
251 TileProgramSwizzleOpaque;
252 typedef ProgramBinding<VertexShaderPosTex, FragmentShaderCheckerboard>
253 TileCheckerboardProgram;
255 // Texture shaders.
256 typedef ProgramBinding<VertexShaderPosTexTransform,
257 FragmentShaderRGBATexVaryingAlpha> TextureProgram;
258 typedef ProgramBinding<VertexShaderPosTexTransform,
259 FragmentShaderRGBATexPremultiplyAlpha>
260 NonPremultipliedTextureProgram;
261 typedef ProgramBinding<VertexShaderPosTexTransform,
262 FragmentShaderTexBackgroundVaryingAlpha>
263 TextureBackgroundProgram;
264 typedef ProgramBinding<VertexShaderPosTexTransform,
265 FragmentShaderTexBackgroundPremultiplyAlpha>
266 NonPremultipliedTextureBackgroundProgram;
267 typedef ProgramBinding<VertexShaderPosTexTransform,
268 FragmentShaderRGBATexRectVaryingAlpha>
269 TextureIOSurfaceProgram;
271 // Render surface shaders.
272 typedef ProgramBinding<VertexShaderPosTexTransform,
273 FragmentShaderRGBATexAlpha> RenderPassProgram;
274 typedef ProgramBinding<VertexShaderPosTexTransform,
275 FragmentShaderRGBATexAlphaMask> RenderPassMaskProgram;
276 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
277 FragmentShaderRGBATexAlphaAA> RenderPassProgramAA;
278 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
279 FragmentShaderRGBATexAlphaMaskAA>
280 RenderPassMaskProgramAA;
281 typedef ProgramBinding<VertexShaderPosTexTransform,
282 FragmentShaderRGBATexColorMatrixAlpha>
283 RenderPassColorMatrixProgram;
284 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
285 FragmentShaderRGBATexAlphaMaskColorMatrixAA>
286 RenderPassMaskColorMatrixProgramAA;
287 typedef ProgramBinding<VertexShaderQuadTexTransformAA,
288 FragmentShaderRGBATexAlphaColorMatrixAA>
289 RenderPassColorMatrixProgramAA;
290 typedef ProgramBinding<VertexShaderPosTexTransform,
291 FragmentShaderRGBATexAlphaMaskColorMatrix>
292 RenderPassMaskColorMatrixProgram;
294 // Video shaders.
295 typedef ProgramBinding<VertexShaderVideoTransform,
296 FragmentShaderOESImageExternal>
297 VideoStreamTextureProgram;
298 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVVideo>
299 VideoYUVProgram;
300 typedef ProgramBinding<VertexShaderPosTexYUVStretch, FragmentShaderYUVAVideo>
301 VideoYUVAProgram;
303 // Special purpose / effects shaders.
304 typedef ProgramBinding<VertexShaderPos, FragmentShaderColor>
305 DebugBorderProgram;
306 typedef ProgramBinding<VertexShaderQuad, FragmentShaderColor>
307 SolidColorProgram;
308 typedef ProgramBinding<VertexShaderQuadAA, FragmentShaderColorAA>
309 SolidColorProgramAA;
311 const TileProgram* GetTileProgram(TexCoordPrecision precision);
312 const TileProgramOpaque* GetTileProgramOpaque(TexCoordPrecision precision);
313 const TileProgramAA* GetTileProgramAA(TexCoordPrecision precision);
314 const TileProgramSwizzle* GetTileProgramSwizzle(TexCoordPrecision precision);
315 const TileProgramSwizzleOpaque* GetTileProgramSwizzleOpaque(
316 TexCoordPrecision precision);
317 const TileProgramSwizzleAA* GetTileProgramSwizzleAA(
318 TexCoordPrecision precision);
319 const TileCheckerboardProgram* GetTileCheckerboardProgram();
321 const RenderPassProgram* GetRenderPassProgram(
322 TexCoordPrecision precision);
323 const RenderPassProgramAA* GetRenderPassProgramAA(
324 TexCoordPrecision precision);
325 const RenderPassMaskProgram* GetRenderPassMaskProgram(
326 TexCoordPrecision precision);
327 const RenderPassMaskProgramAA* GetRenderPassMaskProgramAA(
328 TexCoordPrecision precision);
329 const RenderPassColorMatrixProgram* GetRenderPassColorMatrixProgram(
330 TexCoordPrecision precision);
331 const RenderPassColorMatrixProgramAA* GetRenderPassColorMatrixProgramAA(
332 TexCoordPrecision precision);
333 const RenderPassMaskColorMatrixProgram* GetRenderPassMaskColorMatrixProgram(
334 TexCoordPrecision precision);
335 const RenderPassMaskColorMatrixProgramAA*
336 GetRenderPassMaskColorMatrixProgramAA(TexCoordPrecision precision);
338 const TextureProgram* GetTextureProgram(
339 TexCoordPrecision precision);
340 const NonPremultipliedTextureProgram* GetNonPremultipliedTextureProgram(
341 TexCoordPrecision precision);
342 const TextureBackgroundProgram* GetTextureBackgroundProgram(
343 TexCoordPrecision precision);
344 const NonPremultipliedTextureBackgroundProgram*
345 GetNonPremultipliedTextureBackgroundProgram(TexCoordPrecision precision);
346 const TextureIOSurfaceProgram* GetTextureIOSurfaceProgram(
347 TexCoordPrecision precision);
349 const VideoYUVProgram* GetVideoYUVProgram(
350 TexCoordPrecision precision);
351 const VideoYUVAProgram* GetVideoYUVAProgram(
352 TexCoordPrecision precision);
353 const VideoStreamTextureProgram* GetVideoStreamTextureProgram(
354 TexCoordPrecision precision);
356 const DebugBorderProgram* GetDebugBorderProgram();
357 const SolidColorProgram* GetSolidColorProgram();
358 const SolidColorProgramAA* GetSolidColorProgramAA();
360 scoped_ptr<TileProgram> tile_program_;
361 scoped_ptr<TileProgramOpaque> tile_program_opaque_;
362 scoped_ptr<TileProgramAA> tile_program_aa_;
363 scoped_ptr<TileProgramSwizzle> tile_program_swizzle_;
364 scoped_ptr<TileProgramSwizzleOpaque> tile_program_swizzle_opaque_;
365 scoped_ptr<TileProgramSwizzleAA> tile_program_swizzle_aa_;
366 scoped_ptr<TileCheckerboardProgram> tile_checkerboard_program_;
368 scoped_ptr<TileProgram> tile_program_highp_;
369 scoped_ptr<TileProgramOpaque> tile_program_opaque_highp_;
370 scoped_ptr<TileProgramAA> tile_program_aa_highp_;
371 scoped_ptr<TileProgramSwizzle> tile_program_swizzle_highp_;
372 scoped_ptr<TileProgramSwizzleOpaque> tile_program_swizzle_opaque_highp_;
373 scoped_ptr<TileProgramSwizzleAA> tile_program_swizzle_aa_highp_;
375 scoped_ptr<TextureProgram> texture_program_;
376 scoped_ptr<NonPremultipliedTextureProgram> nonpremultiplied_texture_program_;
377 scoped_ptr<TextureBackgroundProgram> texture_background_program_;
378 scoped_ptr<NonPremultipliedTextureBackgroundProgram>
379 nonpremultiplied_texture_background_program_;
380 scoped_ptr<TextureIOSurfaceProgram> texture_io_surface_program_;
382 scoped_ptr<TextureProgram> texture_program_highp_;
383 scoped_ptr<NonPremultipliedTextureProgram>
384 nonpremultiplied_texture_program_highp_;
385 scoped_ptr<TextureBackgroundProgram> texture_background_program_highp_;
386 scoped_ptr<NonPremultipliedTextureBackgroundProgram>
387 nonpremultiplied_texture_background_program_highp_;
388 scoped_ptr<TextureIOSurfaceProgram> texture_io_surface_program_highp_;
390 scoped_ptr<RenderPassProgram> render_pass_program_;
391 scoped_ptr<RenderPassProgramAA> render_pass_program_aa_;
392 scoped_ptr<RenderPassMaskProgram> render_pass_mask_program_;
393 scoped_ptr<RenderPassMaskProgramAA> render_pass_mask_program_aa_;
394 scoped_ptr<RenderPassColorMatrixProgram> render_pass_color_matrix_program_;
395 scoped_ptr<RenderPassColorMatrixProgramAA>
396 render_pass_color_matrix_program_aa_;
397 scoped_ptr<RenderPassMaskColorMatrixProgram>
398 render_pass_mask_color_matrix_program_;
399 scoped_ptr<RenderPassMaskColorMatrixProgramAA>
400 render_pass_mask_color_matrix_program_aa_;
402 scoped_ptr<RenderPassProgram> render_pass_program_highp_;
403 scoped_ptr<RenderPassProgramAA> render_pass_program_aa_highp_;
404 scoped_ptr<RenderPassMaskProgram> render_pass_mask_program_highp_;
405 scoped_ptr<RenderPassMaskProgramAA> render_pass_mask_program_aa_highp_;
406 scoped_ptr<RenderPassColorMatrixProgram>
407 render_pass_color_matrix_program_highp_;
408 scoped_ptr<RenderPassColorMatrixProgramAA>
409 render_pass_color_matrix_program_aa_highp_;
410 scoped_ptr<RenderPassMaskColorMatrixProgram>
411 render_pass_mask_color_matrix_program_highp_;
412 scoped_ptr<RenderPassMaskColorMatrixProgramAA>
413 render_pass_mask_color_matrix_program_aa_highp_;
415 scoped_ptr<VideoYUVProgram> video_yuv_program_;
416 scoped_ptr<VideoYUVAProgram> video_yuva_program_;
417 scoped_ptr<VideoStreamTextureProgram> video_stream_texture_program_;
419 scoped_ptr<VideoYUVProgram> video_yuv_program_highp_;
420 scoped_ptr<VideoYUVAProgram> video_yuva_program_highp_;
421 scoped_ptr<VideoStreamTextureProgram> video_stream_texture_program_highp_;
423 scoped_ptr<DebugBorderProgram> debug_border_program_;
424 scoped_ptr<SolidColorProgram> solid_color_program_;
425 scoped_ptr<SolidColorProgramAA> solid_color_program_aa_;
427 WebKit::WebGraphicsContext3D* context_;
428 gpu::ContextSupport* context_support_;
430 skia::RefPtr<GrContext> gr_context_;
431 skia::RefPtr<SkCanvas> sk_canvas_;
433 TextureMailboxDeleter* texture_mailbox_deleter_;
435 gfx::Rect swap_buffer_rect_;
436 gfx::Rect scissor_rect_;
437 gfx::Rect viewport_;
438 bool is_backbuffer_discarded_;
439 bool is_using_bind_uniform_;
440 bool visible_;
441 bool is_scissor_enabled_;
442 bool stencil_shadow_;
443 bool blend_shadow_;
444 unsigned program_shadow_;
445 TexturedQuadDrawCache draw_cache_;
446 int highp_threshold_min_;
447 int highp_threshold_cache_;
449 struct PendingAsyncReadPixels;
450 ScopedPtrVector<PendingAsyncReadPixels> pending_async_read_pixels_;
452 scoped_ptr<ResourceProvider::ScopedWriteLockGL> current_framebuffer_lock_;
454 scoped_refptr<ResourceProvider::Fence> last_swap_fence_;
456 SkBitmap on_demand_tile_raster_bitmap_;
457 ResourceProvider::ResourceId on_demand_tile_raster_resource_id_;
459 DISALLOW_COPY_AND_ASSIGN(GLRenderer);
462 // Setting DEBUG_GL_CALLS to 1 will call glGetError() after almost every GL
463 // call made by the compositor. Useful for debugging rendering issues but
464 // will significantly degrade performance.
465 #define DEBUG_GL_CALLS 0
467 #if DEBUG_GL_CALLS && !defined(NDEBUG)
468 #define GLC(context, x) \
469 (x, GLRenderer::DebugGLCall(&* context, #x, __FILE__, __LINE__))
470 #else
471 #define GLC(context, x) (x)
472 #endif
474 } // namespace cc
476 #endif // CC_OUTPUT_GL_RENDERER_H_