[Android] Add a reset_usb utility script (RELAND).
[chromium-blink-merge.git] / cc / output / direct_renderer.cc
blob7ceaa00379f15a96de8b2afcad91ac6367c1a040
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"
7 #include <utility>
8 #include <vector>
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/numerics/safe_conversions.h"
14 #include "base/trace_event/trace_event.h"
15 #include "cc/base/math_util.h"
16 #include "cc/output/bsp_tree.h"
17 #include "cc/output/bsp_walk_action.h"
18 #include "cc/output/copy_output_request.h"
19 #include "cc/quads/draw_quad.h"
20 #include "ui/gfx/geometry/rect_conversions.h"
21 #include "ui/gfx/transform.h"
23 static gfx::Transform OrthoProjectionMatrix(float left,
24 float right,
25 float bottom,
26 float top) {
27 // Use the standard formula to map the clipping frustum to the cube from
28 // [-1, -1, -1] to [1, 1, 1].
29 float delta_x = right - left;
30 float delta_y = top - bottom;
31 gfx::Transform proj;
32 if (!delta_x || !delta_y)
33 return proj;
34 proj.matrix().set(0, 0, 2.0f / delta_x);
35 proj.matrix().set(0, 3, -(right + left) / delta_x);
36 proj.matrix().set(1, 1, 2.0f / delta_y);
37 proj.matrix().set(1, 3, -(top + bottom) / delta_y);
39 // Z component of vertices is always set to zero as we don't use the depth
40 // buffer while drawing.
41 proj.matrix().set(2, 2, 0);
43 return proj;
46 static gfx::Transform window_matrix(int x, int y, int width, int height) {
47 gfx::Transform canvas;
49 // Map to window position and scale up to pixel coordinates.
50 canvas.Translate3d(x, y, 0);
51 canvas.Scale3d(width, height, 0);
53 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
54 canvas.Translate3d(0.5, 0.5, 0.5);
55 canvas.Scale3d(0.5, 0.5, 0.5);
57 return canvas;
60 namespace cc {
62 DirectRenderer::DrawingFrame::DrawingFrame()
63 : root_render_pass(NULL), current_render_pass(NULL), current_texture(NULL) {
66 DirectRenderer::DrawingFrame::~DrawingFrame() {}
69 // static
70 gfx::RectF DirectRenderer::QuadVertexRect() {
71 return gfx::RectF(-0.5f, -0.5f, 1.f, 1.f);
74 // static
75 void DirectRenderer::QuadRectTransform(gfx::Transform* quad_rect_transform,
76 const gfx::Transform& quad_transform,
77 const gfx::RectF& quad_rect) {
78 *quad_rect_transform = quad_transform;
79 quad_rect_transform->Translate(0.5 * quad_rect.width() + quad_rect.x(),
80 0.5 * quad_rect.height() + quad_rect.y());
81 quad_rect_transform->Scale(quad_rect.width(), quad_rect.height());
84 void DirectRenderer::InitializeViewport(DrawingFrame* frame,
85 const gfx::Rect& draw_rect,
86 const gfx::Rect& viewport_rect,
87 const gfx::Size& surface_size) {
88 DCHECK_GE(viewport_rect.x(), 0);
89 DCHECK_GE(viewport_rect.y(), 0);
90 DCHECK_LE(viewport_rect.right(), surface_size.width());
91 DCHECK_LE(viewport_rect.bottom(), surface_size.height());
92 bool flip_y = FlippedFramebuffer(frame);
93 if (flip_y) {
94 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
95 draw_rect.right(),
96 draw_rect.bottom(),
97 draw_rect.y());
98 } else {
99 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
100 draw_rect.right(),
101 draw_rect.y(),
102 draw_rect.bottom());
105 gfx::Rect window_rect = viewport_rect;
106 if (flip_y)
107 window_rect.set_y(surface_size.height() - viewport_rect.bottom());
108 frame->window_matrix = window_matrix(window_rect.x(),
109 window_rect.y(),
110 window_rect.width(),
111 window_rect.height());
112 current_draw_rect_ = draw_rect;
113 current_viewport_rect_ = viewport_rect;
114 current_surface_size_ = surface_size;
115 current_window_space_viewport_ = window_rect;
118 gfx::Rect DirectRenderer::MoveFromDrawToWindowSpace(
119 const DrawingFrame* frame,
120 const gfx::Rect& draw_rect) const {
121 gfx::Rect window_rect = draw_rect;
122 window_rect -= current_draw_rect_.OffsetFromOrigin();
123 window_rect += current_viewport_rect_.OffsetFromOrigin();
124 if (FlippedFramebuffer(frame))
125 window_rect.set_y(current_surface_size_.height() - window_rect.bottom());
126 return window_rect;
129 DirectRenderer::DirectRenderer(RendererClient* client,
130 const RendererSettings* settings,
131 OutputSurface* output_surface,
132 ResourceProvider* resource_provider)
133 : Renderer(client, settings),
134 output_surface_(output_surface),
135 resource_provider_(resource_provider),
136 overlay_processor_(new OverlayProcessor(output_surface)) {
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);
162 continue;
165 gfx::Size required_size = it->second;
166 ScopedResource* texture = pass_iter->second;
167 DCHECK(texture);
169 bool size_appropriate = texture->size().width() >= required_size.width() &&
170 texture->size().height() >= required_size.height();
171 if (texture->id() && !size_appropriate)
172 texture->Free();
175 // Delete RenderPass textures from the previous frame that will not be used
176 // again.
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,
185 texture.Pass());
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(
197 "Renderer4.renderPassCount",
198 base::saturated_cast<int>(render_passes_in_draw_order->size()));
200 const RenderPass* root_render_pass = render_passes_in_draw_order->back();
201 DCHECK(root_render_pass);
203 DrawingFrame frame;
204 frame.render_passes_in_draw_order = render_passes_in_draw_order;
205 frame.root_render_pass = root_render_pass;
206 frame.root_damage_rect = Capabilities().using_partial_swap
207 ? root_render_pass->damage_rect
208 : root_render_pass->output_rect;
209 frame.root_damage_rect.Intersect(gfx::Rect(device_viewport_rect.size()));
210 frame.device_viewport_rect = device_viewport_rect;
211 frame.device_clip_rect = device_clip_rect;
212 frame.disable_picture_quad_image_filtering =
213 disable_picture_quad_image_filtering;
215 EnsureBackbuffer();
217 // Only reshape when we know we are going to draw. Otherwise, the reshape
218 // can leave the window at the wrong size if we never draw and the proper
219 // viewport size is never set.
220 output_surface_->Reshape(device_viewport_rect.size(), device_scale_factor);
222 BeginDrawingFrame(&frame);
224 // If we have any copy requests, we can't remove any quads for overlays,
225 // otherwise the framebuffer will be missing the overlay contents.
226 if (root_render_pass->copy_requests.empty()) {
227 overlay_processor_->ProcessForOverlays(render_passes_in_draw_order,
228 &frame.overlay_list);
231 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
232 RenderPass* pass = render_passes_in_draw_order->at(i);
233 DrawRenderPass(&frame, pass);
235 for (ScopedPtrVector<CopyOutputRequest>::iterator it =
236 pass->copy_requests.begin();
237 it != pass->copy_requests.end();
238 ++it) {
239 if (it != pass->copy_requests.begin()) {
240 // Doing a readback is destructive of our state on Mac, so make sure
241 // we restore the state between readbacks. http://crbug.com/99393.
242 UseRenderPass(&frame, pass);
244 CopyCurrentRenderPassToBitmap(&frame, pass->copy_requests.take(it));
247 FinishDrawingFrame(&frame);
249 render_passes_in_draw_order->clear();
252 gfx::Rect DirectRenderer::ComputeScissorRectForRenderPass(
253 const DrawingFrame* frame) {
254 gfx::Rect render_pass_scissor = frame->current_render_pass->output_rect;
256 if (frame->root_damage_rect == frame->root_render_pass->output_rect ||
257 !frame->current_render_pass->copy_requests.empty())
258 return render_pass_scissor;
260 gfx::Transform inverse_transform(gfx::Transform::kSkipInitialization);
261 if (frame->current_render_pass->transform_to_root_target.GetInverse(
262 &inverse_transform)) {
263 // Only intersect inverse-projected damage if the transform is invertible.
264 gfx::Rect damage_rect_in_render_pass_space =
265 MathUtil::ProjectEnclosingClippedRect(inverse_transform,
266 frame->root_damage_rect);
267 render_pass_scissor.Intersect(damage_rect_in_render_pass_space);
270 return render_pass_scissor;
273 bool DirectRenderer::NeedDeviceClip(const DrawingFrame* frame) const {
274 if (frame->current_render_pass != frame->root_render_pass)
275 return false;
277 return !frame->device_clip_rect.Contains(frame->device_viewport_rect);
280 gfx::Rect DirectRenderer::DeviceClipRectInDrawSpace(
281 const DrawingFrame* frame) const {
282 gfx::Rect device_clip_rect = frame->device_clip_rect;
283 device_clip_rect -= current_viewport_rect_.OffsetFromOrigin();
284 device_clip_rect += current_draw_rect_.OffsetFromOrigin();
285 return device_clip_rect;
288 gfx::Rect DirectRenderer::DeviceViewportRectInDrawSpace(
289 const DrawingFrame* frame) const {
290 gfx::Rect device_viewport_rect = frame->device_viewport_rect;
291 device_viewport_rect -= current_viewport_rect_.OffsetFromOrigin();
292 device_viewport_rect += current_draw_rect_.OffsetFromOrigin();
293 return device_viewport_rect;
296 gfx::Rect DirectRenderer::OutputSurfaceRectInDrawSpace(
297 const DrawingFrame* frame) const {
298 if (frame->current_render_pass == frame->root_render_pass) {
299 gfx::Rect output_surface_rect(output_surface_->SurfaceSize());
300 output_surface_rect -= current_viewport_rect_.OffsetFromOrigin();
301 output_surface_rect += current_draw_rect_.OffsetFromOrigin();
302 return output_surface_rect;
303 } else {
304 return frame->current_render_pass->output_rect;
308 bool DirectRenderer::ShouldSkipQuad(const DrawQuad& quad,
309 const gfx::Rect& render_pass_scissor) {
310 if (render_pass_scissor.IsEmpty())
311 return true;
313 if (quad.shared_quad_state->is_clipped) {
314 gfx::Rect r = quad.shared_quad_state->clip_rect;
315 r.Intersect(render_pass_scissor);
316 return r.IsEmpty();
319 return false;
322 void DirectRenderer::SetScissorStateForQuad(
323 const DrawingFrame* frame,
324 const DrawQuad& quad,
325 const gfx::Rect& render_pass_scissor,
326 bool use_render_pass_scissor) {
327 if (use_render_pass_scissor) {
328 gfx::Rect quad_scissor_rect = render_pass_scissor;
329 if (quad.shared_quad_state->is_clipped)
330 quad_scissor_rect.Intersect(quad.shared_quad_state->clip_rect);
331 SetScissorTestRectInDrawSpace(frame, quad_scissor_rect);
332 return;
333 } else if (quad.shared_quad_state->is_clipped) {
334 SetScissorTestRectInDrawSpace(frame, quad.shared_quad_state->clip_rect);
335 return;
338 EnsureScissorTestDisabled();
341 void DirectRenderer::SetScissorTestRectInDrawSpace(
342 const DrawingFrame* frame,
343 const gfx::Rect& draw_space_rect) {
344 gfx::Rect window_space_rect =
345 MoveFromDrawToWindowSpace(frame, draw_space_rect);
346 SetScissorTestRect(window_space_rect);
349 void DirectRenderer::FinishDrawingQuadList() {}
351 void DirectRenderer::DoDrawPolygon(const DrawPolygon& poly,
352 DrawingFrame* frame,
353 const gfx::Rect& render_pass_scissor,
354 bool use_render_pass_scissor) {
355 SetScissorStateForQuad(frame, *poly.original_ref(), render_pass_scissor,
356 use_render_pass_scissor);
358 // If the poly has not been split, then it is just a normal DrawQuad,
359 // and we should save any extra processing that would have to be done.
360 if (!poly.is_split()) {
361 DoDrawQuad(frame, poly.original_ref(), NULL);
362 return;
365 std::vector<gfx::QuadF> quads;
366 poly.ToQuads2D(&quads);
367 for (size_t i = 0; i < quads.size(); ++i) {
368 DoDrawQuad(frame, poly.original_ref(), &quads[i]);
372 void DirectRenderer::FlushPolygons(ScopedPtrDeque<DrawPolygon>* poly_list,
373 DrawingFrame* frame,
374 const gfx::Rect& render_pass_scissor,
375 bool use_render_pass_scissor) {
376 if (poly_list->empty()) {
377 return;
380 BspTree bsp_tree(poly_list);
381 BspWalkActionDrawPolygon action_handler(this, frame, render_pass_scissor,
382 use_render_pass_scissor);
383 bsp_tree.TraverseWithActionHandler(&action_handler);
384 DCHECK(poly_list->empty());
387 void DirectRenderer::DrawRenderPass(DrawingFrame* frame,
388 const RenderPass* render_pass) {
389 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
390 if (!UseRenderPass(frame, render_pass))
391 return;
393 const gfx::Rect surface_rect_in_draw_space =
394 OutputSurfaceRectInDrawSpace(frame);
395 gfx::Rect render_pass_scissor_in_draw_space = surface_rect_in_draw_space;
397 if (frame->current_render_pass == frame->root_render_pass) {
398 render_pass_scissor_in_draw_space.Intersect(
399 DeviceViewportRectInDrawSpace(frame));
402 if (Capabilities().using_partial_swap) {
403 render_pass_scissor_in_draw_space.Intersect(
404 ComputeScissorRectForRenderPass(frame));
407 if (NeedDeviceClip(frame)) {
408 render_pass_scissor_in_draw_space.Intersect(
409 DeviceClipRectInDrawSpace(frame));
412 bool render_pass_is_clipped =
413 !render_pass_scissor_in_draw_space.Contains(surface_rect_in_draw_space);
414 bool is_root_render_pass =
415 frame->current_render_pass == frame->root_render_pass;
416 bool has_external_stencil_test =
417 is_root_render_pass && output_surface_->HasExternalStencilTest();
418 bool should_clear_surface =
419 !has_external_stencil_test &&
420 (!is_root_render_pass || settings_->should_clear_root_render_pass);
422 // If |has_external_stencil_test| we can't discard or clear. Make sure we
423 // don't need to.
424 DCHECK_IMPLIES(has_external_stencil_test,
425 !frame->current_render_pass->has_transparent_background);
427 SurfaceInitializationMode mode;
428 if (should_clear_surface && render_pass_is_clipped) {
429 mode = SURFACE_INITIALIZATION_MODE_SCISSORED_CLEAR;
430 } else if (should_clear_surface) {
431 mode = SURFACE_INITIALIZATION_MODE_FULL_SURFACE_CLEAR;
432 } else {
433 mode = SURFACE_INITIALIZATION_MODE_PRESERVE;
436 PrepareSurfaceForPass(
437 frame, mode,
438 MoveFromDrawToWindowSpace(frame, render_pass_scissor_in_draw_space));
440 const QuadList& quad_list = render_pass->quad_list;
441 ScopedPtrDeque<DrawPolygon> poly_list;
443 int next_polygon_id = 0;
444 int last_sorting_context_id = 0;
445 for (auto it = quad_list.BackToFrontBegin(); it != quad_list.BackToFrontEnd();
446 ++it) {
447 const DrawQuad& quad = **it;
448 gfx::QuadF send_quad(quad.visible_rect);
450 if (render_pass_is_clipped &&
451 ShouldSkipQuad(quad, render_pass_scissor_in_draw_space)) {
452 continue;
455 if (last_sorting_context_id != quad.shared_quad_state->sorting_context_id) {
456 last_sorting_context_id = quad.shared_quad_state->sorting_context_id;
457 FlushPolygons(&poly_list, frame, render_pass_scissor_in_draw_space,
458 render_pass_is_clipped);
461 // This layer is in a 3D sorting context so we add it to the list of
462 // polygons to go into the BSP tree.
463 if (quad.shared_quad_state->sorting_context_id != 0) {
464 scoped_ptr<DrawPolygon> new_polygon(new DrawPolygon(
465 *it, quad.visible_rect,
466 quad.shared_quad_state->quad_to_target_transform, next_polygon_id++));
467 if (new_polygon->points().size() > 2u) {
468 poly_list.push_back(new_polygon.Pass());
470 continue;
473 // We are not in a 3d sorting context, so we should draw the quad normally.
474 SetScissorStateForQuad(frame, quad, render_pass_scissor_in_draw_space,
475 render_pass_is_clipped);
477 DoDrawQuad(frame, &quad, nullptr);
479 FlushPolygons(&poly_list, frame, render_pass_scissor_in_draw_space,
480 render_pass_is_clipped);
481 FinishDrawingQuadList();
484 bool DirectRenderer::UseRenderPass(DrawingFrame* frame,
485 const RenderPass* render_pass) {
486 frame->current_render_pass = render_pass;
487 frame->current_texture = NULL;
488 if (render_pass == frame->root_render_pass) {
489 BindFramebufferToOutputSurface(frame);
490 InitializeViewport(frame,
491 render_pass->output_rect,
492 frame->device_viewport_rect,
493 output_surface_->SurfaceSize());
494 return true;
497 ScopedResource* texture = render_pass_textures_.get(render_pass->id);
498 DCHECK(texture);
500 gfx::Size size = RenderPassTextureSize(render_pass);
501 size.Enlarge(enlarge_pass_texture_amount_.x(),
502 enlarge_pass_texture_amount_.y());
503 if (!texture->id()) {
504 texture->Allocate(
505 size, ResourceProvider::TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER, RGBA_8888);
507 DCHECK(texture->id());
509 if (BindFramebufferToTexture(frame, texture, render_pass->output_rect)) {
510 InitializeViewport(frame, render_pass->output_rect,
511 gfx::Rect(render_pass->output_rect.size()),
512 render_pass->output_rect.size());
513 return true;
516 return false;
519 bool DirectRenderer::HasAllocatedResourcesForTesting(RenderPassId id) const {
520 ScopedResource* texture = render_pass_textures_.get(id);
521 return texture && texture->id();
524 // static
525 gfx::Size DirectRenderer::RenderPassTextureSize(const RenderPass* render_pass) {
526 return render_pass->output_rect.size();
529 } // namespace cc