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/containers/hash_tables.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/metrics/histogram.h"
13 #include "base/trace_event/trace_event.h"
14 #include "cc/base/math_util.h"
15 #include "cc/output/bsp_tree.h"
16 #include "cc/output/bsp_walk_action.h"
17 #include "cc/output/copy_output_request.h"
18 #include "cc/quads/draw_quad.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/transform.h"
22 static gfx::Transform
OrthoProjectionMatrix(float left
,
26 // Use the standard formula to map the clipping frustum to the cube from
27 // [-1, -1, -1] to [1, 1, 1].
28 float delta_x
= right
- left
;
29 float delta_y
= top
- bottom
;
31 if (!delta_x
|| !delta_y
)
33 proj
.matrix().set(0, 0, 2.0f
/ delta_x
);
34 proj
.matrix().set(0, 3, -(right
+ left
) / delta_x
);
35 proj
.matrix().set(1, 1, 2.0f
/ delta_y
);
36 proj
.matrix().set(1, 3, -(top
+ bottom
) / delta_y
);
38 // Z component of vertices is always set to zero as we don't use the depth
39 // buffer while drawing.
40 proj
.matrix().set(2, 2, 0);
45 static gfx::Transform
window_matrix(int x
, int y
, int width
, int height
) {
46 gfx::Transform canvas
;
48 // Map to window position and scale up to pixel coordinates.
49 canvas
.Translate3d(x
, y
, 0);
50 canvas
.Scale3d(width
, height
, 0);
52 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
53 canvas
.Translate3d(0.5, 0.5, 0.5);
54 canvas
.Scale3d(0.5, 0.5, 0.5);
61 DirectRenderer::DrawingFrame::DrawingFrame()
62 : root_render_pass(NULL
), current_render_pass(NULL
), current_texture(NULL
) {
65 DirectRenderer::DrawingFrame::~DrawingFrame() {}
69 gfx::RectF
DirectRenderer::QuadVertexRect() {
70 return gfx::RectF(-0.5f
, -0.5f
, 1.f
, 1.f
);
74 void DirectRenderer::QuadRectTransform(gfx::Transform
* quad_rect_transform
,
75 const gfx::Transform
& quad_transform
,
76 const gfx::RectF
& quad_rect
) {
77 *quad_rect_transform
= quad_transform
;
78 quad_rect_transform
->Translate(0.5 * quad_rect
.width() + quad_rect
.x(),
79 0.5 * quad_rect
.height() + quad_rect
.y());
80 quad_rect_transform
->Scale(quad_rect
.width(), quad_rect
.height());
83 void DirectRenderer::InitializeViewport(DrawingFrame
* frame
,
84 const gfx::Rect
& draw_rect
,
85 const gfx::Rect
& viewport_rect
,
86 const gfx::Size
& surface_size
) {
87 DCHECK_GE(viewport_rect
.x(), 0);
88 DCHECK_GE(viewport_rect
.y(), 0);
89 DCHECK_LE(viewport_rect
.right(), surface_size
.width());
90 DCHECK_LE(viewport_rect
.bottom(), surface_size
.height());
91 bool flip_y
= FlippedFramebuffer(frame
);
93 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
98 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
104 gfx::Rect window_rect
= viewport_rect
;
106 window_rect
.set_y(surface_size
.height() - viewport_rect
.bottom());
107 frame
->window_matrix
= window_matrix(window_rect
.x(),
110 window_rect
.height());
111 current_draw_rect_
= draw_rect
;
112 current_viewport_rect_
= viewport_rect
;
113 current_surface_size_
= surface_size
;
114 current_window_space_viewport_
= window_rect
;
117 gfx::Rect
DirectRenderer::MoveFromDrawToWindowSpace(
118 const DrawingFrame
* frame
,
119 const gfx::Rect
& draw_rect
) const {
120 gfx::Rect window_rect
= draw_rect
;
121 window_rect
-= current_draw_rect_
.OffsetFromOrigin();
122 window_rect
+= current_viewport_rect_
.OffsetFromOrigin();
123 if (FlippedFramebuffer(frame
))
124 window_rect
.set_y(current_surface_size_
.height() - window_rect
.bottom());
128 DirectRenderer::DirectRenderer(RendererClient
* client
,
129 const RendererSettings
* settings
,
130 OutputSurface
* output_surface
,
131 ResourceProvider
* resource_provider
)
132 : Renderer(client
, settings
),
133 output_surface_(output_surface
),
134 resource_provider_(resource_provider
),
136 new OverlayProcessor(output_surface
, resource_provider
)) {
137 overlay_processor_
->Initialize();
140 DirectRenderer::~DirectRenderer() {}
142 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
143 const gfx::Vector2d
& amount
) {
144 enlarge_pass_texture_amount_
= amount
;
147 void DirectRenderer::DecideRenderPassAllocationsForFrame(
148 const RenderPassList
& render_passes_in_draw_order
) {
149 base::hash_map
<RenderPassId
, gfx::Size
> render_passes_in_frame
;
150 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
)
151 render_passes_in_frame
.insert(std::pair
<RenderPassId
, gfx::Size
>(
152 render_passes_in_draw_order
[i
]->id
,
153 RenderPassTextureSize(render_passes_in_draw_order
[i
])));
155 std::vector
<RenderPassId
> passes_to_delete
;
156 for (auto pass_iter
= render_pass_textures_
.begin();
157 pass_iter
!= render_pass_textures_
.end(); ++pass_iter
) {
158 base::hash_map
<RenderPassId
, gfx::Size
>::const_iterator it
=
159 render_passes_in_frame
.find(pass_iter
->first
);
160 if (it
== render_passes_in_frame
.end()) {
161 passes_to_delete
.push_back(pass_iter
->first
);
165 gfx::Size required_size
= it
->second
;
166 ScopedResource
* texture
= pass_iter
->second
;
169 bool size_appropriate
= texture
->size().width() >= required_size
.width() &&
170 texture
->size().height() >= required_size
.height();
171 if (texture
->id() && !size_appropriate
)
175 // Delete RenderPass textures from the previous frame that will not be used
177 for (size_t i
= 0; i
< passes_to_delete
.size(); ++i
)
178 render_pass_textures_
.erase(passes_to_delete
[i
]);
180 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
) {
181 if (!render_pass_textures_
.contains(render_passes_in_draw_order
[i
]->id
)) {
182 scoped_ptr
<ScopedResource
> texture
=
183 ScopedResource::Create(resource_provider_
);
184 render_pass_textures_
.set(render_passes_in_draw_order
[i
]->id
,
190 void DirectRenderer::DrawFrame(RenderPassList
* render_passes_in_draw_order
,
191 float device_scale_factor
,
192 const gfx::Rect
& device_viewport_rect
,
193 const gfx::Rect
& device_clip_rect
,
194 bool disable_picture_quad_image_filtering
) {
195 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
196 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
197 render_passes_in_draw_order
->size());
199 const RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
200 DCHECK(root_render_pass
);
203 frame
.render_passes_in_draw_order
= render_passes_in_draw_order
;
204 frame
.root_render_pass
= root_render_pass
;
205 frame
.root_damage_rect
= Capabilities().using_partial_swap
206 ? root_render_pass
->damage_rect
207 : root_render_pass
->output_rect
;
208 frame
.root_damage_rect
.Intersect(gfx::Rect(device_viewport_rect
.size()));
209 frame
.device_viewport_rect
= device_viewport_rect
;
210 frame
.device_clip_rect
= device_clip_rect
;
211 frame
.disable_picture_quad_image_filtering
=
212 disable_picture_quad_image_filtering
;
214 if (root_render_pass
->copy_requests
.empty()) {
215 // If we have any copy requests, we can't remove any quads for overlays,
216 // otherwise the framebuffer will be missing the overlay contents.
217 overlay_processor_
->ProcessForOverlays(render_passes_in_draw_order
,
218 &frame
.overlay_list
);
223 // Only reshape when we know we are going to draw. Otherwise, the reshape
224 // can leave the window at the wrong size if we never draw and the proper
225 // viewport size is never set.
226 output_surface_
->Reshape(device_viewport_rect
.size(), device_scale_factor
);
228 BeginDrawingFrame(&frame
);
229 for (size_t i
= 0; i
< render_passes_in_draw_order
->size(); ++i
) {
230 RenderPass
* pass
= render_passes_in_draw_order
->at(i
);
231 DrawRenderPass(&frame
, pass
);
233 for (ScopedPtrVector
<CopyOutputRequest
>::iterator it
=
234 pass
->copy_requests
.begin();
235 it
!= pass
->copy_requests
.end();
237 if (it
!= pass
->copy_requests
.begin()) {
238 // Doing a readback is destructive of our state on Mac, so make sure
239 // we restore the state between readbacks. http://crbug.com/99393.
240 UseRenderPass(&frame
, pass
);
242 CopyCurrentRenderPassToBitmap(&frame
, pass
->copy_requests
.take(it
));
245 FinishDrawingFrame(&frame
);
247 render_passes_in_draw_order
->clear();
250 gfx::Rect
DirectRenderer::ComputeScissorRectForRenderPass(
251 const DrawingFrame
* frame
) {
252 gfx::Rect render_pass_scissor
= frame
->current_render_pass
->output_rect
;
254 if (frame
->root_damage_rect
== frame
->root_render_pass
->output_rect
||
255 !frame
->current_render_pass
->copy_requests
.empty())
256 return render_pass_scissor
;
258 gfx::Transform
inverse_transform(gfx::Transform::kSkipInitialization
);
259 if (frame
->current_render_pass
->transform_to_root_target
.GetInverse(
260 &inverse_transform
)) {
261 // Only intersect inverse-projected damage if the transform is invertible.
262 gfx::Rect damage_rect_in_render_pass_space
=
263 MathUtil::ProjectEnclosingClippedRect(inverse_transform
,
264 frame
->root_damage_rect
);
265 render_pass_scissor
.Intersect(damage_rect_in_render_pass_space
);
268 return render_pass_scissor
;
271 bool DirectRenderer::NeedDeviceClip(const DrawingFrame
* frame
) const {
272 if (frame
->current_render_pass
!= frame
->root_render_pass
)
275 return !frame
->device_clip_rect
.Contains(frame
->device_viewport_rect
);
278 gfx::Rect
DirectRenderer::DeviceClipRectInDrawSpace(
279 const DrawingFrame
* frame
) const {
280 gfx::Rect device_clip_rect
= frame
->device_clip_rect
;
281 device_clip_rect
-= current_viewport_rect_
.OffsetFromOrigin();
282 device_clip_rect
+= current_draw_rect_
.OffsetFromOrigin();
283 return device_clip_rect
;
286 gfx::Rect
DirectRenderer::DeviceViewportRectInDrawSpace(
287 const DrawingFrame
* frame
) const {
288 gfx::Rect device_viewport_rect
= frame
->device_viewport_rect
;
289 device_viewport_rect
-= current_viewport_rect_
.OffsetFromOrigin();
290 device_viewport_rect
+= current_draw_rect_
.OffsetFromOrigin();
291 return device_viewport_rect
;
294 gfx::Rect
DirectRenderer::OutputSurfaceRectInDrawSpace(
295 const DrawingFrame
* frame
) const {
296 if (frame
->current_render_pass
== frame
->root_render_pass
) {
297 gfx::Rect
output_surface_rect(output_surface_
->SurfaceSize());
298 output_surface_rect
-= current_viewport_rect_
.OffsetFromOrigin();
299 output_surface_rect
+= current_draw_rect_
.OffsetFromOrigin();
300 return output_surface_rect
;
302 return frame
->current_render_pass
->output_rect
;
306 bool DirectRenderer::ShouldSkipQuad(const DrawQuad
& quad
,
307 const gfx::Rect
& render_pass_scissor
) {
308 if (render_pass_scissor
.IsEmpty())
311 if (quad
.isClipped()) {
312 gfx::Rect r
= quad
.clipRect();
313 r
.Intersect(render_pass_scissor
);
320 void DirectRenderer::SetScissorStateForQuad(
321 const DrawingFrame
* frame
,
322 const DrawQuad
& quad
,
323 const gfx::Rect
& render_pass_scissor
,
324 bool use_render_pass_scissor
) {
325 if (use_render_pass_scissor
) {
326 gfx::Rect quad_scissor_rect
= render_pass_scissor
;
327 if (quad
.isClipped())
328 quad_scissor_rect
.Intersect(quad
.clipRect());
329 SetScissorTestRectInDrawSpace(frame
, quad_scissor_rect
);
331 } else if (quad
.isClipped()) {
332 SetScissorTestRectInDrawSpace(frame
, quad
.clipRect());
336 EnsureScissorTestDisabled();
339 void DirectRenderer::SetScissorTestRectInDrawSpace(
340 const DrawingFrame
* frame
,
341 const gfx::Rect
& draw_space_rect
) {
342 gfx::Rect window_space_rect
=
343 MoveFromDrawToWindowSpace(frame
, draw_space_rect
);
344 SetScissorTestRect(window_space_rect
);
347 void DirectRenderer::FinishDrawingQuadList() {}
349 void DirectRenderer::DoDrawPolygon(const DrawPolygon
& poly
,
351 const gfx::Rect
& render_pass_scissor
,
352 bool use_render_pass_scissor
) {
353 SetScissorStateForQuad(frame
, *poly
.original_ref(), render_pass_scissor
,
354 use_render_pass_scissor
);
356 // If the poly has not been split, then it is just a normal DrawQuad,
357 // and we should save any extra processing that would have to be done.
358 if (!poly
.is_split()) {
359 DoDrawQuad(frame
, poly
.original_ref(), NULL
);
363 std::vector
<gfx::QuadF
> quads
;
364 poly
.ToQuads2D(&quads
);
365 for (size_t i
= 0; i
< quads
.size(); ++i
) {
366 DoDrawQuad(frame
, poly
.original_ref(), &quads
[i
]);
370 void DirectRenderer::FlushPolygons(ScopedPtrDeque
<DrawPolygon
>* poly_list
,
372 const gfx::Rect
& render_pass_scissor
,
373 bool use_render_pass_scissor
) {
374 if (poly_list
->empty()) {
378 BspTree
bsp_tree(poly_list
);
379 BspWalkActionDrawPolygon
action_handler(this, frame
, render_pass_scissor
,
380 use_render_pass_scissor
);
381 bsp_tree
.TraverseWithActionHandler(&action_handler
);
382 DCHECK(poly_list
->empty());
385 void DirectRenderer::DrawRenderPass(DrawingFrame
* frame
,
386 const RenderPass
* render_pass
) {
387 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
388 if (!UseRenderPass(frame
, render_pass
))
391 const gfx::Rect surface_rect_in_draw_space
=
392 OutputSurfaceRectInDrawSpace(frame
);
393 gfx::Rect render_pass_scissor_in_draw_space
= surface_rect_in_draw_space
;
395 if (frame
->current_render_pass
== frame
->root_render_pass
) {
396 render_pass_scissor_in_draw_space
.Intersect(
397 DeviceViewportRectInDrawSpace(frame
));
400 if (Capabilities().using_partial_swap
) {
401 render_pass_scissor_in_draw_space
.Intersect(
402 ComputeScissorRectForRenderPass(frame
));
405 if (NeedDeviceClip(frame
)) {
406 render_pass_scissor_in_draw_space
.Intersect(
407 DeviceClipRectInDrawSpace(frame
));
410 bool render_pass_is_clipped
=
411 !render_pass_scissor_in_draw_space
.Contains(surface_rect_in_draw_space
);
412 bool is_root_render_pass
=
413 frame
->current_render_pass
== frame
->root_render_pass
;
414 bool has_external_stencil_test
=
415 is_root_render_pass
&& output_surface_
->HasExternalStencilTest();
416 bool should_clear_surface
=
417 !has_external_stencil_test
&&
418 (!is_root_render_pass
|| settings_
->should_clear_root_render_pass
);
420 // If |has_external_stencil_test| we can't discard or clear. Make sure we
422 DCHECK_IMPLIES(has_external_stencil_test
,
423 !frame
->current_render_pass
->has_transparent_background
);
425 SurfaceInitializationMode mode
;
426 if (should_clear_surface
&& render_pass_is_clipped
) {
427 mode
= SURFACE_INITIALIZATION_MODE_SCISSORED_CLEAR
;
428 } else if (should_clear_surface
) {
429 mode
= SURFACE_INITIALIZATION_MODE_FULL_SURFACE_CLEAR
;
431 mode
= SURFACE_INITIALIZATION_MODE_PRESERVE
;
434 PrepareSurfaceForPass(
436 MoveFromDrawToWindowSpace(frame
, render_pass_scissor_in_draw_space
));
438 const QuadList
& quad_list
= render_pass
->quad_list
;
439 ScopedPtrDeque
<DrawPolygon
> poly_list
;
441 int next_polygon_id
= 0;
442 int last_sorting_context_id
= 0;
443 for (auto it
= quad_list
.BackToFrontBegin(); it
!= quad_list
.BackToFrontEnd();
445 const DrawQuad
& quad
= **it
;
446 gfx::QuadF
send_quad(quad
.visible_rect
);
448 if (render_pass_is_clipped
&&
449 ShouldSkipQuad(quad
, render_pass_scissor_in_draw_space
)) {
453 if (last_sorting_context_id
!= quad
.shared_quad_state
->sorting_context_id
) {
454 last_sorting_context_id
= quad
.shared_quad_state
->sorting_context_id
;
455 FlushPolygons(&poly_list
, frame
, render_pass_scissor_in_draw_space
,
456 render_pass_is_clipped
);
459 // This layer is in a 3D sorting context so we add it to the list of
460 // polygons to go into the BSP tree.
461 if (quad
.shared_quad_state
->sorting_context_id
!= 0) {
462 scoped_ptr
<DrawPolygon
> new_polygon(new DrawPolygon(
463 *it
, quad
.visible_rect
, quad
.quadTransform(), next_polygon_id
++));
464 if (new_polygon
->points().size() > 2u) {
465 poly_list
.push_back(new_polygon
.Pass());
470 // We are not in a 3d sorting context, so we should draw the quad normally.
471 SetScissorStateForQuad(frame
, quad
, render_pass_scissor_in_draw_space
,
472 render_pass_is_clipped
);
474 DoDrawQuad(frame
, &quad
, nullptr);
476 FlushPolygons(&poly_list
, frame
, render_pass_scissor_in_draw_space
,
477 render_pass_is_clipped
);
478 FinishDrawingQuadList();
481 bool DirectRenderer::UseRenderPass(DrawingFrame
* frame
,
482 const RenderPass
* render_pass
) {
483 frame
->current_render_pass
= render_pass
;
484 frame
->current_texture
= NULL
;
485 if (render_pass
== frame
->root_render_pass
) {
486 BindFramebufferToOutputSurface(frame
);
487 InitializeViewport(frame
,
488 render_pass
->output_rect
,
489 frame
->device_viewport_rect
,
490 output_surface_
->SurfaceSize());
494 ScopedResource
* texture
= render_pass_textures_
.get(render_pass
->id
);
497 gfx::Size size
= RenderPassTextureSize(render_pass
);
498 size
.Enlarge(enlarge_pass_texture_amount_
.x(),
499 enlarge_pass_texture_amount_
.y());
500 if (!texture
->id()) {
502 size
, ResourceProvider::TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER
, RGBA_8888
);
504 DCHECK(texture
->id());
506 if (BindFramebufferToTexture(frame
, texture
, render_pass
->output_rect
)) {
507 InitializeViewport(frame
, render_pass
->output_rect
,
508 gfx::Rect(render_pass
->output_rect
.size()),
509 render_pass
->output_rect
.size());
516 bool DirectRenderer::HasAllocatedResourcesForTesting(RenderPassId id
) const {
517 ScopedResource
* texture
= render_pass_textures_
.get(id
);
518 return texture
&& texture
->id();
522 gfx::Size
DirectRenderer::RenderPassTextureSize(const RenderPass
* render_pass
) {
523 return render_pass
->output_rect
.size();