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 #include "cc/output/direct_renderer.h"
10 #include "base/debug/trace_event.h"
11 #include "base/metrics/histogram.h"
12 #include "cc/base/math_util.h"
13 #include "cc/quads/draw_quad.h"
14 #include "ui/gfx/rect_conversions.h"
15 #include "ui/gfx/transform.h"
17 static gfx::Transform
OrthoProjectionMatrix(float left
,
21 // Use the standard formula to map the clipping frustum to the cube from
22 // [-1, -1, -1] to [1, 1, 1].
23 float delta_x
= right
- left
;
24 float delta_y
= top
- bottom
;
26 if (!delta_x
|| !delta_y
)
28 proj
.matrix().setDouble(0, 0, 2.0f
/ delta_x
);
29 proj
.matrix().setDouble(0, 3, -(right
+ left
) / delta_x
);
30 proj
.matrix().setDouble(1, 1, 2.0f
/ delta_y
);
31 proj
.matrix().setDouble(1, 3, -(top
+ bottom
) / delta_y
);
33 // Z component of vertices is always set to zero as we don't use the depth
34 // buffer while drawing.
35 proj
.matrix().setDouble(2, 2, 0);
40 static gfx::Transform
window_matrix(int x
, int y
, int width
, int height
) {
41 gfx::Transform canvas
;
43 // Map to window position and scale up to pixel coordinates.
44 canvas
.Translate3d(x
, y
, 0);
45 canvas
.Scale3d(width
, height
, 0);
47 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
48 canvas
.Translate3d(0.5, 0.5, 0.5);
49 canvas
.Scale3d(0.5, 0.5, 0.5);
56 DirectRenderer::DrawingFrame::DrawingFrame()
57 : root_render_pass(NULL
),
58 current_render_pass(NULL
),
59 current_texture(NULL
),
62 DirectRenderer::DrawingFrame::~DrawingFrame() {}
66 gfx::RectF
DirectRenderer::QuadVertexRect() {
67 return gfx::RectF(-0.5f
, -0.5f
, 1.f
, 1.f
);
71 void DirectRenderer::QuadRectTransform(gfx::Transform
* quad_rect_transform
,
72 const gfx::Transform
& quad_transform
,
73 const gfx::RectF
& quad_rect
) {
74 *quad_rect_transform
= quad_transform
;
75 quad_rect_transform
->Translate(0.5 * quad_rect
.width() + quad_rect
.x(),
76 0.5 * quad_rect
.height() + quad_rect
.y());
77 quad_rect_transform
->Scale(quad_rect
.width(), quad_rect
.height());
81 void DirectRenderer::InitializeMatrices(DrawingFrame
* frame
,
85 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
90 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
95 frame
->window_matrix
=
96 window_matrix(0, 0, draw_rect
.width(), draw_rect
.height());
97 frame
->flipped_y
= flip_y
;
101 gfx::Rect
DirectRenderer::MoveScissorToWindowSpace(
102 const DrawingFrame
* frame
, const gfx::RectF
& scissor_rect
) {
103 gfx::Rect scissor_rect_in_canvas_space
= gfx::ToEnclosingRect(scissor_rect
);
104 // The scissor coordinates must be supplied in viewport space so we need to
105 // offset by the relative position of the top left corner of the current
107 gfx::Rect framebuffer_output_rect
= frame
->current_render_pass
->output_rect
;
108 scissor_rect_in_canvas_space
.set_x(
109 scissor_rect_in_canvas_space
.x() - framebuffer_output_rect
.x());
110 if (frame
->flipped_y
&& !frame
->current_texture
) {
111 scissor_rect_in_canvas_space
.set_y(
112 framebuffer_output_rect
.height() -
113 (scissor_rect_in_canvas_space
.bottom() - framebuffer_output_rect
.y()));
115 scissor_rect_in_canvas_space
.set_y(
116 scissor_rect_in_canvas_space
.y() - framebuffer_output_rect
.y());
118 return scissor_rect_in_canvas_space
;
121 DirectRenderer::DirectRenderer(RendererClient
* client
,
122 ResourceProvider
* resource_provider
)
124 resource_provider_(resource_provider
) {}
126 DirectRenderer::~DirectRenderer() {}
128 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
129 gfx::Vector2d amount
) {
130 enlarge_pass_texture_amount_
= amount
;
133 void DirectRenderer::DecideRenderPassAllocationsForFrame(
134 const RenderPassList
& render_passes_in_draw_order
) {
135 base::hash_map
<RenderPass::Id
, const RenderPass
*> render_passes_in_frame
;
136 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
)
137 render_passes_in_frame
.insert(std::pair
<RenderPass::Id
, const RenderPass
*>(
138 render_passes_in_draw_order
[i
]->id
, render_passes_in_draw_order
[i
]));
140 std::vector
<RenderPass::Id
> passes_to_delete
;
141 ScopedPtrHashMap
<RenderPass::Id
, CachedResource
>::const_iterator pass_iter
;
142 for (pass_iter
= render_pass_textures_
.begin();
143 pass_iter
!= render_pass_textures_
.end();
145 base::hash_map
<RenderPass::Id
, const RenderPass
*>::const_iterator it
=
146 render_passes_in_frame
.find(pass_iter
->first
);
147 if (it
== render_passes_in_frame
.end()) {
148 passes_to_delete
.push_back(pass_iter
->first
);
152 const RenderPass
* render_pass_in_frame
= it
->second
;
153 gfx::Size required_size
= RenderPassTextureSize(render_pass_in_frame
);
154 GLenum required_format
= RenderPassTextureFormat(render_pass_in_frame
);
155 CachedResource
* texture
= pass_iter
->second
;
158 bool size_appropriate
= texture
->size().width() >= required_size
.width() &&
159 texture
->size().height() >= required_size
.height();
161 (!size_appropriate
|| texture
->format() != required_format
))
165 // Delete RenderPass textures from the previous frame that will not be used
167 for (size_t i
= 0; i
< passes_to_delete
.size(); ++i
)
168 render_pass_textures_
.erase(passes_to_delete
[i
]);
170 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
) {
171 if (!render_pass_textures_
.contains(render_passes_in_draw_order
[i
]->id
)) {
172 scoped_ptr
<CachedResource
> texture
=
173 CachedResource::Create(resource_provider_
);
174 render_pass_textures_
.set(render_passes_in_draw_order
[i
]->id
,
180 void DirectRenderer::DrawFrame(RenderPassList
* render_passes_in_draw_order
) {
181 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
182 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
183 render_passes_in_draw_order
->size());
185 const RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
186 DCHECK(root_render_pass
);
189 frame
.root_render_pass
= root_render_pass
;
190 frame
.root_damage_rect
=
191 Capabilities().using_partial_swap
&& client_
->AllowPartialSwap() ?
192 root_render_pass
->damage_rect
: root_render_pass
->output_rect
;
193 frame
.root_damage_rect
.Intersect(gfx::Rect(ViewportSize()));
195 BeginDrawingFrame(&frame
);
196 for (size_t i
= 0; i
< render_passes_in_draw_order
->size(); ++i
)
197 DrawRenderPass(&frame
, render_passes_in_draw_order
->at(i
));
198 FinishDrawingFrame(&frame
);
200 render_passes_in_draw_order
->clear();
203 gfx::RectF
DirectRenderer::ComputeScissorRectForRenderPass(
204 const DrawingFrame
* frame
) {
205 gfx::RectF render_pass_scissor
= frame
->current_render_pass
->output_rect
;
207 if (frame
->root_damage_rect
== frame
->root_render_pass
->output_rect
)
208 return render_pass_scissor
;
210 gfx::Transform
inverse_transform(gfx::Transform::kSkipInitialization
);
211 if (frame
->current_render_pass
->transform_to_root_target
.GetInverse(
212 &inverse_transform
)) {
213 // Only intersect inverse-projected damage if the transform is invertible.
214 gfx::RectF damage_rect_in_render_pass_space
=
215 MathUtil::ProjectClippedRect(inverse_transform
,
216 frame
->root_damage_rect
);
217 render_pass_scissor
.Intersect(damage_rect_in_render_pass_space
);
220 return render_pass_scissor
;
223 void DirectRenderer::SetScissorStateForQuad(const DrawingFrame
* frame
,
224 const DrawQuad
& quad
) {
225 if (quad
.isClipped()) {
226 gfx::RectF quad_scissor_rect
= quad
.clipRect();
227 SetScissorTestRect(MoveScissorToWindowSpace(frame
, quad_scissor_rect
));
229 EnsureScissorTestDisabled();
233 void DirectRenderer::SetScissorStateForQuadWithRenderPassScissor(
234 const DrawingFrame
* frame
,
235 const DrawQuad
& quad
,
236 const gfx::RectF
& render_pass_scissor
,
237 bool* should_skip_quad
) {
238 gfx::RectF quad_scissor_rect
= render_pass_scissor
;
240 if (quad
.isClipped())
241 quad_scissor_rect
.Intersect(quad
.clipRect());
243 if (quad_scissor_rect
.IsEmpty()) {
244 *should_skip_quad
= true;
248 *should_skip_quad
= false;
249 SetScissorTestRect(MoveScissorToWindowSpace(frame
, quad_scissor_rect
));
252 void DirectRenderer::FinishDrawingQuadList() {}
254 void DirectRenderer::DrawRenderPass(DrawingFrame
* frame
,
255 const RenderPass
* render_pass
) {
256 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
257 if (!UseRenderPass(frame
, render_pass
))
260 bool using_scissor_as_optimization
=
261 Capabilities().using_partial_swap
&& client_
->AllowPartialSwap();
262 gfx::RectF render_pass_scissor
;
264 if (using_scissor_as_optimization
) {
265 render_pass_scissor
= ComputeScissorRectForRenderPass(frame
);
266 SetScissorTestRect(MoveScissorToWindowSpace(frame
, render_pass_scissor
));
269 if (frame
->current_render_pass
!= frame
->root_render_pass
||
270 client_
->ShouldClearRootRenderPass()) {
271 if (!using_scissor_as_optimization
)
272 EnsureScissorTestDisabled();
273 ClearFramebuffer(frame
);
276 const QuadList
& quad_list
= render_pass
->quad_list
;
277 for (QuadList::ConstBackToFrontIterator it
= quad_list
.BackToFrontBegin();
278 it
!= quad_list
.BackToFrontEnd();
280 const DrawQuad
& quad
= *(*it
);
281 bool should_skip_quad
= false;
283 if (using_scissor_as_optimization
) {
284 SetScissorStateForQuadWithRenderPassScissor(
285 frame
, quad
, render_pass_scissor
, &should_skip_quad
);
287 SetScissorStateForQuad(frame
, quad
);
290 if (!should_skip_quad
)
291 DoDrawQuad(frame
, *it
);
293 FinishDrawingQuadList();
295 CachedResource
* texture
= render_pass_textures_
.get(render_pass
->id
);
297 texture
->set_is_complete(
298 !render_pass
->has_occlusion_from_outside_target_surface
);
302 bool DirectRenderer::UseRenderPass(DrawingFrame
* frame
,
303 const RenderPass
* render_pass
) {
304 frame
->current_render_pass
= render_pass
;
305 frame
->current_texture
= NULL
;
307 if (render_pass
== frame
->root_render_pass
) {
308 BindFramebufferToOutputSurface(frame
);
309 InitializeMatrices(frame
, render_pass
->output_rect
, FlippedFramebuffer());
310 SetDrawViewportSize(render_pass
->output_rect
.size());
314 CachedResource
* texture
= render_pass_textures_
.get(render_pass
->id
);
317 gfx::Size size
= RenderPassTextureSize(render_pass
);
318 size
.Enlarge(enlarge_pass_texture_amount_
.x(),
319 enlarge_pass_texture_amount_
.y());
320 if (!texture
->id() &&
321 !texture
->Allocate(size
,
322 RenderPassTextureFormat(render_pass
),
323 ResourceProvider::TextureUsageFramebuffer
))
326 return BindFramebufferToTexture(frame
, texture
, render_pass
->output_rect
);
329 bool DirectRenderer::HaveCachedResourcesForRenderPassId(RenderPass::Id id
)
331 if (!Settings().cache_render_pass_contents
)
334 CachedResource
* texture
= render_pass_textures_
.get(id
);
335 return texture
&& texture
->id() && texture
->is_complete();
339 gfx::Size
DirectRenderer::RenderPassTextureSize(const RenderPass
* render_pass
) {
340 return render_pass
->output_rect
.size();
344 GLenum
DirectRenderer::RenderPassTextureFormat(const RenderPass
* render_pass
) {