1 // Copyright 2012 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_DIRECT_RENDERER_H_
6 #define CC_OUTPUT_DIRECT_RENDERER_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/containers/scoped_ptr_hash_map.h"
11 #include "cc/base/cc_export.h"
12 #include "cc/output/renderer.h"
13 #include "cc/resources/resource_provider.h"
14 #include "cc/resources/scoped_resource.h"
18 class ResourceProvider
;
20 // This is the base class for code shared between the GL and software
21 // renderer implementations. "Direct" refers to the fact that it does not
22 // delegate rendering to another compositor.
23 class CC_EXPORT DirectRenderer
: public Renderer
{
25 virtual ~DirectRenderer();
27 ResourceProvider
* resource_provider() const { return resource_provider_
; }
29 virtual bool CanReadPixels() const OVERRIDE
;
30 virtual void DecideRenderPassAllocationsForFrame(
31 const RenderPassList
& render_passes_in_draw_order
) OVERRIDE
;
32 virtual bool HaveCachedResourcesForRenderPassId(RenderPass::Id id
) const
34 virtual void DrawFrame(RenderPassList
* render_passes_in_draw_order
,
35 ContextProvider
* offscreen_context_provider
) OVERRIDE
;
37 struct CC_EXPORT DrawingFrame
{
41 const RenderPass
* root_render_pass
;
42 const RenderPass
* current_render_pass
;
43 const ScopedResource
* current_texture
;
45 gfx::RectF root_damage_rect
;
47 gfx::Transform projection_matrix
;
48 gfx::Transform window_matrix
;
50 ContextProvider
* offscreen_context_provider
;
53 void SetEnlargePassTextureAmountForTesting(gfx::Vector2d amount
);
56 DirectRenderer(RendererClient
* client
,
57 OutputSurface
* output_surface
,
58 ResourceProvider
* resource_provider
);
60 class CachedResource
: public ScopedResource
{
62 static scoped_ptr
<CachedResource
> Create(
63 ResourceProvider
* resource_provider
) {
64 return make_scoped_ptr(new CachedResource(resource_provider
));
66 virtual ~CachedResource() {}
68 bool is_complete() const { return is_complete_
; }
69 void set_is_complete(bool is_complete
) { is_complete_
= is_complete
; }
72 explicit CachedResource(ResourceProvider
* resource_provider
)
73 : ScopedResource(resource_provider
),
74 is_complete_(false) {}
79 DISALLOW_COPY_AND_ASSIGN(CachedResource
);
82 static gfx::RectF
QuadVertexRect();
83 static void QuadRectTransform(gfx::Transform
* quad_rect_transform
,
84 const gfx::Transform
& quad_transform
,
85 const gfx::RectF
& quad_rect
);
86 void InitializeViewport(DrawingFrame
* frame
,
88 gfx::Rect viewport_rect
,
89 gfx::Size surface_size
);
90 gfx::Rect
MoveFromDrawToWindowSpace(const gfx::RectF
& draw_rect
) const;
92 static gfx::RectF
ComputeScissorRectForRenderPass(const DrawingFrame
* frame
);
93 void SetScissorStateForQuad(const DrawingFrame
* frame
, const DrawQuad
& quad
);
94 void SetScissorStateForQuadWithRenderPassScissor(
95 const DrawingFrame
* frame
,
97 const gfx::RectF
& render_pass_scissor
,
98 bool* should_skip_quad
);
100 static gfx::Size
RenderPassTextureSize(const RenderPass
* render_pass
);
101 static GLenum
RenderPassTextureFormat(const RenderPass
* render_pass
);
103 void DrawRenderPass(DrawingFrame
* frame
, const RenderPass
* render_pass
);
104 bool UseRenderPass(DrawingFrame
* frame
, const RenderPass
* render_pass
);
106 virtual void BindFramebufferToOutputSurface(DrawingFrame
* frame
) = 0;
107 virtual bool BindFramebufferToTexture(DrawingFrame
* frame
,
108 const ScopedResource
* resource
,
109 gfx::Rect target_rect
) = 0;
110 virtual void SetDrawViewport(gfx::Rect window_space_viewport
) = 0;
111 virtual void SetScissorTestRect(gfx::Rect scissor_rect
) = 0;
112 virtual void ClearFramebuffer(DrawingFrame
* frame
) = 0;
113 virtual void DoDrawQuad(DrawingFrame
* frame
, const DrawQuad
* quad
) = 0;
114 virtual void BeginDrawingFrame(DrawingFrame
* frame
) = 0;
115 virtual void FinishDrawingFrame(DrawingFrame
* frame
) = 0;
116 virtual void FinishDrawingQuadList();
117 virtual bool FlippedFramebuffer() const = 0;
118 virtual void EnsureScissorTestEnabled() = 0;
119 virtual void EnsureScissorTestDisabled() = 0;
120 virtual void DiscardBackbuffer() {}
121 virtual void EnsureBackbuffer() {}
123 virtual void CopyCurrentRenderPassToBitmap(
125 scoped_ptr
<CopyOutputRequest
> request
) = 0;
127 base::ScopedPtrHashMap
<RenderPass::Id
, CachedResource
> render_pass_textures_
;
128 OutputSurface
* output_surface_
;
129 ResourceProvider
* resource_provider_
;
131 // For use in coordinate conversion, this stores the output rect, viewport
132 // rect (= unflipped version of glViewport rect), and the size of target
133 // framebuffer. During a draw, this stores the values for the current render
134 // pass; in between draws, they retain the values for the root render pass of
136 gfx::Rect current_draw_rect_
;
137 gfx::Rect current_viewport_rect_
;
138 gfx::Size current_surface_size_
;
141 gfx::Vector2d enlarge_pass_texture_amount_
;
143 DISALLOW_COPY_AND_ASSIGN(DirectRenderer
);
148 #endif // CC_OUTPUT_DIRECT_RENDERER_H_