[Android WebView] Put AndroidStreamReaderURLRequestJob into a namespace
[chromium-blink-merge.git] / cc / output / direct_renderer.cc
blob58facc4f12111f65d061d0ae4a22f2234d5865a8
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/quad_f.h"
21 #include "ui/gfx/geometry/rect_conversions.h"
22 #include "ui/gfx/transform.h"
24 static gfx::Transform OrthoProjectionMatrix(float left,
25 float right,
26 float bottom,
27 float top) {
28 // Use the standard formula to map the clipping frustum to the cube from
29 // [-1, -1, -1] to [1, 1, 1].
30 float delta_x = right - left;
31 float delta_y = top - bottom;
32 gfx::Transform proj;
33 if (!delta_x || !delta_y)
34 return proj;
35 proj.matrix().set(0, 0, 2.0f / delta_x);
36 proj.matrix().set(0, 3, -(right + left) / delta_x);
37 proj.matrix().set(1, 1, 2.0f / delta_y);
38 proj.matrix().set(1, 3, -(top + bottom) / delta_y);
40 // Z component of vertices is always set to zero as we don't use the depth
41 // buffer while drawing.
42 proj.matrix().set(2, 2, 0);
44 return proj;
47 static gfx::Transform window_matrix(int x, int y, int width, int height) {
48 gfx::Transform canvas;
50 // Map to window position and scale up to pixel coordinates.
51 canvas.Translate3d(x, y, 0);
52 canvas.Scale3d(width, height, 0);
54 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
55 canvas.Translate3d(0.5, 0.5, 0.5);
56 canvas.Scale3d(0.5, 0.5, 0.5);
58 return canvas;
61 namespace cc {
63 DirectRenderer::DrawingFrame::DrawingFrame()
64 : root_render_pass(NULL), current_render_pass(NULL), current_texture(NULL) {
67 DirectRenderer::DrawingFrame::~DrawingFrame() {}
70 // static
71 gfx::RectF DirectRenderer::QuadVertexRect() {
72 return gfx::RectF(-0.5f, -0.5f, 1.f, 1.f);
75 // static
76 void DirectRenderer::QuadRectTransform(gfx::Transform* quad_rect_transform,
77 const gfx::Transform& quad_transform,
78 const gfx::RectF& quad_rect) {
79 *quad_rect_transform = quad_transform;
80 quad_rect_transform->Translate(0.5 * quad_rect.width() + quad_rect.x(),
81 0.5 * quad_rect.height() + quad_rect.y());
82 quad_rect_transform->Scale(quad_rect.width(), quad_rect.height());
85 void DirectRenderer::InitializeViewport(DrawingFrame* frame,
86 const gfx::Rect& draw_rect,
87 const gfx::Rect& viewport_rect,
88 const gfx::Size& surface_size) {
89 DCHECK_GE(viewport_rect.x(), 0);
90 DCHECK_GE(viewport_rect.y(), 0);
91 DCHECK_LE(viewport_rect.right(), surface_size.width());
92 DCHECK_LE(viewport_rect.bottom(), surface_size.height());
93 bool flip_y = FlippedFramebuffer(frame);
94 if (flip_y) {
95 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
96 draw_rect.right(),
97 draw_rect.bottom(),
98 draw_rect.y());
99 } else {
100 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
101 draw_rect.right(),
102 draw_rect.y(),
103 draw_rect.bottom());
106 gfx::Rect window_rect = viewport_rect;
107 if (flip_y)
108 window_rect.set_y(surface_size.height() - viewport_rect.bottom());
109 frame->window_matrix = window_matrix(window_rect.x(),
110 window_rect.y(),
111 window_rect.width(),
112 window_rect.height());
113 current_draw_rect_ = draw_rect;
114 current_viewport_rect_ = viewport_rect;
115 current_surface_size_ = surface_size;
116 current_window_space_viewport_ = window_rect;
119 gfx::Rect DirectRenderer::MoveFromDrawToWindowSpace(
120 const DrawingFrame* frame,
121 const gfx::Rect& draw_rect) const {
122 gfx::Rect window_rect = draw_rect;
123 window_rect -= current_draw_rect_.OffsetFromOrigin();
124 window_rect += current_viewport_rect_.OffsetFromOrigin();
125 if (FlippedFramebuffer(frame))
126 window_rect.set_y(current_surface_size_.height() - window_rect.bottom());
127 return window_rect;
130 DirectRenderer::DirectRenderer(RendererClient* client,
131 const RendererSettings* settings,
132 OutputSurface* output_surface,
133 ResourceProvider* resource_provider)
134 : Renderer(client, settings),
135 output_surface_(output_surface),
136 resource_provider_(resource_provider),
137 overlay_processor_(new OverlayProcessor(output_surface)) {
138 overlay_processor_->Initialize();
141 DirectRenderer::~DirectRenderer() {}
143 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
144 const gfx::Vector2d& amount) {
145 enlarge_pass_texture_amount_ = amount;
148 void DirectRenderer::DecideRenderPassAllocationsForFrame(
149 const RenderPassList& render_passes_in_draw_order) {
150 base::hash_map<RenderPassId, gfx::Size> render_passes_in_frame;
151 for (size_t i = 0; i < render_passes_in_draw_order.size(); ++i)
152 render_passes_in_frame.insert(std::pair<RenderPassId, gfx::Size>(
153 render_passes_in_draw_order[i]->id,
154 RenderPassTextureSize(render_passes_in_draw_order[i])));
156 std::vector<RenderPassId> passes_to_delete;
157 for (auto pass_iter = render_pass_textures_.begin();
158 pass_iter != render_pass_textures_.end(); ++pass_iter) {
159 base::hash_map<RenderPassId, gfx::Size>::const_iterator it =
160 render_passes_in_frame.find(pass_iter->first);
161 if (it == render_passes_in_frame.end()) {
162 passes_to_delete.push_back(pass_iter->first);
163 continue;
166 gfx::Size required_size = it->second;
167 ScopedResource* texture = pass_iter->second;
168 DCHECK(texture);
170 bool size_appropriate = texture->size().width() >= required_size.width() &&
171 texture->size().height() >= required_size.height();
172 if (texture->id() && !size_appropriate)
173 texture->Free();
176 // Delete RenderPass textures from the previous frame that will not be used
177 // again.
178 for (size_t i = 0; i < passes_to_delete.size(); ++i)
179 render_pass_textures_.erase(passes_to_delete[i]);
181 for (size_t i = 0; i < render_passes_in_draw_order.size(); ++i) {
182 if (!render_pass_textures_.contains(render_passes_in_draw_order[i]->id)) {
183 scoped_ptr<ScopedResource> texture =
184 ScopedResource::Create(resource_provider_);
185 render_pass_textures_.set(render_passes_in_draw_order[i]->id,
186 texture.Pass());
191 void DirectRenderer::DrawFrame(RenderPassList* render_passes_in_draw_order,
192 float device_scale_factor,
193 const gfx::Rect& device_viewport_rect,
194 const gfx::Rect& device_clip_rect,
195 bool disable_picture_quad_image_filtering) {
196 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
197 UMA_HISTOGRAM_COUNTS(
198 "Renderer4.renderPassCount",
199 base::saturated_cast<int>(render_passes_in_draw_order->size()));
201 const RenderPass* root_render_pass = render_passes_in_draw_order->back();
202 DCHECK(root_render_pass);
204 DrawingFrame frame;
205 frame.render_passes_in_draw_order = render_passes_in_draw_order;
206 frame.root_render_pass = root_render_pass;
207 frame.root_damage_rect = Capabilities().using_partial_swap
208 ? root_render_pass->damage_rect
209 : root_render_pass->output_rect;
210 frame.root_damage_rect.Intersect(gfx::Rect(device_viewport_rect.size()));
211 frame.device_viewport_rect = device_viewport_rect;
212 frame.device_clip_rect = device_clip_rect;
213 frame.disable_picture_quad_image_filtering =
214 disable_picture_quad_image_filtering;
216 EnsureBackbuffer();
218 // Only reshape when we know we are going to draw. Otherwise, the reshape
219 // can leave the window at the wrong size if we never draw and the proper
220 // viewport size is never set.
221 output_surface_->Reshape(device_viewport_rect.size(), device_scale_factor);
223 BeginDrawingFrame(&frame);
225 // If we have any copy requests, we can't remove any quads for overlays,
226 // otherwise the framebuffer will be missing the overlay contents.
227 if (root_render_pass->copy_requests.empty()) {
228 overlay_processor_->ProcessForOverlays(render_passes_in_draw_order,
229 &frame.overlay_list);
232 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
233 RenderPass* pass = render_passes_in_draw_order->at(i);
234 DrawRenderPass(&frame, pass);
236 for (ScopedPtrVector<CopyOutputRequest>::iterator it =
237 pass->copy_requests.begin();
238 it != pass->copy_requests.end();
239 ++it) {
240 if (it != pass->copy_requests.begin()) {
241 // Doing a readback is destructive of our state on Mac, so make sure
242 // we restore the state between readbacks. http://crbug.com/99393.
243 UseRenderPass(&frame, pass);
245 CopyCurrentRenderPassToBitmap(&frame, pass->copy_requests.take(it));
248 FinishDrawingFrame(&frame);
250 render_passes_in_draw_order->clear();
253 gfx::Rect DirectRenderer::ComputeScissorRectForRenderPass(
254 const DrawingFrame* frame) {
255 gfx::Rect render_pass_scissor = frame->current_render_pass->output_rect;
257 if (frame->root_damage_rect == frame->root_render_pass->output_rect ||
258 !frame->current_render_pass->copy_requests.empty())
259 return render_pass_scissor;
261 gfx::Transform inverse_transform(gfx::Transform::kSkipInitialization);
262 if (frame->current_render_pass->transform_to_root_target.GetInverse(
263 &inverse_transform)) {
264 // Only intersect inverse-projected damage if the transform is invertible.
265 gfx::Rect damage_rect_in_render_pass_space =
266 MathUtil::ProjectEnclosingClippedRect(inverse_transform,
267 frame->root_damage_rect);
268 render_pass_scissor.Intersect(damage_rect_in_render_pass_space);
271 return render_pass_scissor;
274 bool DirectRenderer::NeedDeviceClip(const DrawingFrame* frame) const {
275 if (frame->current_render_pass != frame->root_render_pass)
276 return false;
278 return !frame->device_clip_rect.Contains(frame->device_viewport_rect);
281 gfx::Rect DirectRenderer::DeviceClipRectInDrawSpace(
282 const DrawingFrame* frame) const {
283 gfx::Rect device_clip_rect = frame->device_clip_rect;
284 device_clip_rect -= current_viewport_rect_.OffsetFromOrigin();
285 device_clip_rect += current_draw_rect_.OffsetFromOrigin();
286 return device_clip_rect;
289 gfx::Rect DirectRenderer::DeviceViewportRectInDrawSpace(
290 const DrawingFrame* frame) const {
291 gfx::Rect device_viewport_rect = frame->device_viewport_rect;
292 device_viewport_rect -= current_viewport_rect_.OffsetFromOrigin();
293 device_viewport_rect += current_draw_rect_.OffsetFromOrigin();
294 return device_viewport_rect;
297 gfx::Rect DirectRenderer::OutputSurfaceRectInDrawSpace(
298 const DrawingFrame* frame) const {
299 if (frame->current_render_pass == frame->root_render_pass) {
300 gfx::Rect output_surface_rect(output_surface_->SurfaceSize());
301 output_surface_rect -= current_viewport_rect_.OffsetFromOrigin();
302 output_surface_rect += current_draw_rect_.OffsetFromOrigin();
303 return output_surface_rect;
304 } else {
305 return frame->current_render_pass->output_rect;
309 bool DirectRenderer::ShouldSkipQuad(const DrawQuad& quad,
310 const gfx::Rect& render_pass_scissor) {
311 if (render_pass_scissor.IsEmpty())
312 return true;
314 if (quad.shared_quad_state->is_clipped) {
315 gfx::Rect r = quad.shared_quad_state->clip_rect;
316 r.Intersect(render_pass_scissor);
317 return r.IsEmpty();
320 return false;
323 void DirectRenderer::SetScissorStateForQuad(
324 const DrawingFrame* frame,
325 const DrawQuad& quad,
326 const gfx::Rect& render_pass_scissor,
327 bool use_render_pass_scissor) {
328 if (use_render_pass_scissor) {
329 gfx::Rect quad_scissor_rect = render_pass_scissor;
330 if (quad.shared_quad_state->is_clipped)
331 quad_scissor_rect.Intersect(quad.shared_quad_state->clip_rect);
332 SetScissorTestRectInDrawSpace(frame, quad_scissor_rect);
333 return;
334 } else if (quad.shared_quad_state->is_clipped) {
335 SetScissorTestRectInDrawSpace(frame, quad.shared_quad_state->clip_rect);
336 return;
339 EnsureScissorTestDisabled();
342 void DirectRenderer::SetScissorTestRectInDrawSpace(
343 const DrawingFrame* frame,
344 const gfx::Rect& draw_space_rect) {
345 gfx::Rect window_space_rect =
346 MoveFromDrawToWindowSpace(frame, draw_space_rect);
347 SetScissorTestRect(window_space_rect);
350 void DirectRenderer::FinishDrawingQuadList() {}
352 void DirectRenderer::DoDrawPolygon(const DrawPolygon& poly,
353 DrawingFrame* frame,
354 const gfx::Rect& render_pass_scissor,
355 bool use_render_pass_scissor) {
356 SetScissorStateForQuad(frame, *poly.original_ref(), render_pass_scissor,
357 use_render_pass_scissor);
359 // If the poly has not been split, then it is just a normal DrawQuad,
360 // and we should save any extra processing that would have to be done.
361 if (!poly.is_split()) {
362 DoDrawQuad(frame, poly.original_ref(), NULL);
363 return;
366 std::vector<gfx::QuadF> quads;
367 poly.ToQuads2D(&quads);
368 for (size_t i = 0; i < quads.size(); ++i) {
369 DoDrawQuad(frame, poly.original_ref(), &quads[i]);
373 void DirectRenderer::FlushPolygons(ScopedPtrDeque<DrawPolygon>* poly_list,
374 DrawingFrame* frame,
375 const gfx::Rect& render_pass_scissor,
376 bool use_render_pass_scissor) {
377 if (poly_list->empty()) {
378 return;
381 BspTree bsp_tree(poly_list);
382 BspWalkActionDrawPolygon action_handler(this, frame, render_pass_scissor,
383 use_render_pass_scissor);
384 bsp_tree.TraverseWithActionHandler(&action_handler);
385 DCHECK(poly_list->empty());
388 void DirectRenderer::DrawRenderPass(DrawingFrame* frame,
389 const RenderPass* render_pass) {
390 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
391 if (!UseRenderPass(frame, render_pass))
392 return;
394 const gfx::Rect surface_rect_in_draw_space =
395 OutputSurfaceRectInDrawSpace(frame);
396 gfx::Rect render_pass_scissor_in_draw_space = surface_rect_in_draw_space;
398 if (frame->current_render_pass == frame->root_render_pass) {
399 render_pass_scissor_in_draw_space.Intersect(
400 DeviceViewportRectInDrawSpace(frame));
403 if (Capabilities().using_partial_swap) {
404 render_pass_scissor_in_draw_space.Intersect(
405 ComputeScissorRectForRenderPass(frame));
408 if (NeedDeviceClip(frame)) {
409 render_pass_scissor_in_draw_space.Intersect(
410 DeviceClipRectInDrawSpace(frame));
413 bool render_pass_is_clipped =
414 !render_pass_scissor_in_draw_space.Contains(surface_rect_in_draw_space);
415 bool is_root_render_pass =
416 frame->current_render_pass == frame->root_render_pass;
417 bool has_external_stencil_test =
418 is_root_render_pass && output_surface_->HasExternalStencilTest();
419 bool should_clear_surface =
420 !has_external_stencil_test &&
421 (!is_root_render_pass || settings_->should_clear_root_render_pass);
423 // If |has_external_stencil_test| we can't discard or clear. Make sure we
424 // don't need to.
425 DCHECK_IMPLIES(has_external_stencil_test,
426 !frame->current_render_pass->has_transparent_background);
428 SurfaceInitializationMode mode;
429 if (should_clear_surface && render_pass_is_clipped) {
430 mode = SURFACE_INITIALIZATION_MODE_SCISSORED_CLEAR;
431 } else if (should_clear_surface) {
432 mode = SURFACE_INITIALIZATION_MODE_FULL_SURFACE_CLEAR;
433 } else {
434 mode = SURFACE_INITIALIZATION_MODE_PRESERVE;
437 PrepareSurfaceForPass(
438 frame, mode,
439 MoveFromDrawToWindowSpace(frame, render_pass_scissor_in_draw_space));
441 const QuadList& quad_list = render_pass->quad_list;
442 ScopedPtrDeque<DrawPolygon> poly_list;
444 int next_polygon_id = 0;
445 int last_sorting_context_id = 0;
446 for (auto it = quad_list.BackToFrontBegin(); it != quad_list.BackToFrontEnd();
447 ++it) {
448 const DrawQuad& quad = **it;
449 gfx::QuadF send_quad(gfx::RectF(quad.visible_rect));
451 if (render_pass_is_clipped &&
452 ShouldSkipQuad(quad, render_pass_scissor_in_draw_space)) {
453 continue;
456 if (last_sorting_context_id != quad.shared_quad_state->sorting_context_id) {
457 last_sorting_context_id = quad.shared_quad_state->sorting_context_id;
458 FlushPolygons(&poly_list, frame, render_pass_scissor_in_draw_space,
459 render_pass_is_clipped);
462 // This layer is in a 3D sorting context so we add it to the list of
463 // polygons to go into the BSP tree.
464 if (quad.shared_quad_state->sorting_context_id != 0) {
465 scoped_ptr<DrawPolygon> new_polygon(new DrawPolygon(
466 *it, gfx::RectF(quad.visible_rect),
467 quad.shared_quad_state->quad_to_target_transform, next_polygon_id++));
468 if (new_polygon->points().size() > 2u) {
469 poly_list.push_back(new_polygon.Pass());
471 continue;
474 // We are not in a 3d sorting context, so we should draw the quad normally.
475 SetScissorStateForQuad(frame, quad, render_pass_scissor_in_draw_space,
476 render_pass_is_clipped);
478 DoDrawQuad(frame, &quad, nullptr);
480 FlushPolygons(&poly_list, frame, render_pass_scissor_in_draw_space,
481 render_pass_is_clipped);
482 FinishDrawingQuadList();
485 bool DirectRenderer::UseRenderPass(DrawingFrame* frame,
486 const RenderPass* render_pass) {
487 frame->current_render_pass = render_pass;
488 frame->current_texture = NULL;
489 if (render_pass == frame->root_render_pass) {
490 BindFramebufferToOutputSurface(frame);
491 InitializeViewport(frame,
492 render_pass->output_rect,
493 frame->device_viewport_rect,
494 output_surface_->SurfaceSize());
495 return true;
498 ScopedResource* texture = render_pass_textures_.get(render_pass->id);
499 DCHECK(texture);
501 gfx::Size size = RenderPassTextureSize(render_pass);
502 size.Enlarge(enlarge_pass_texture_amount_.x(),
503 enlarge_pass_texture_amount_.y());
504 if (!texture->id()) {
505 texture->Allocate(
506 size, ResourceProvider::TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER, RGBA_8888);
508 DCHECK(texture->id());
510 if (BindFramebufferToTexture(frame, texture, render_pass->output_rect)) {
511 InitializeViewport(frame, render_pass->output_rect,
512 gfx::Rect(render_pass->output_rect.size()),
513 render_pass->output_rect.size());
514 return true;
517 return false;
520 bool DirectRenderer::HasAllocatedResourcesForTesting(RenderPassId id) const {
521 ScopedResource* texture = render_pass_textures_.get(id);
522 return texture && texture->id();
525 // static
526 gfx::Size DirectRenderer::RenderPassTextureSize(const RenderPass* render_pass) {
527 return render_pass->output_rect.size();
530 } // namespace cc