Dismiss autofill popup on screen orientation change.
[chromium-blink-merge.git] / cc / output / direct_renderer.cc
bloba664d7df822836f734567d4f3489749fb716a5e6
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/debug/trace_event.h"
12 #include "base/metrics/histogram.h"
13 #include "cc/base/math_util.h"
14 #include "cc/output/copy_output_request.h"
15 #include "cc/quads/draw_quad.h"
16 #include "ui/gfx/rect_conversions.h"
17 #include "ui/gfx/transform.h"
19 static gfx::Transform OrthoProjectionMatrix(float left,
20 float right,
21 float bottom,
22 float top) {
23 // Use the standard formula to map the clipping frustum to the cube from
24 // [-1, -1, -1] to [1, 1, 1].
25 float delta_x = right - left;
26 float delta_y = top - bottom;
27 gfx::Transform proj;
28 if (!delta_x || !delta_y)
29 return proj;
30 proj.matrix().setDouble(0, 0, 2.0f / delta_x);
31 proj.matrix().setDouble(0, 3, -(right + left) / delta_x);
32 proj.matrix().setDouble(1, 1, 2.0f / delta_y);
33 proj.matrix().setDouble(1, 3, -(top + bottom) / delta_y);
35 // Z component of vertices is always set to zero as we don't use the depth
36 // buffer while drawing.
37 proj.matrix().setDouble(2, 2, 0);
39 return proj;
42 static gfx::Transform window_matrix(int x, int y, int width, int height) {
43 gfx::Transform canvas;
45 // Map to window position and scale up to pixel coordinates.
46 canvas.Translate3d(x, y, 0);
47 canvas.Scale3d(width, height, 0);
49 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
50 canvas.Translate3d(0.5, 0.5, 0.5);
51 canvas.Scale3d(0.5, 0.5, 0.5);
53 return canvas;
56 namespace cc {
58 DirectRenderer::DrawingFrame::DrawingFrame()
59 : root_render_pass(NULL),
60 current_render_pass(NULL),
61 current_texture(NULL) {}
63 DirectRenderer::DrawingFrame::~DrawingFrame() {}
66 // static
67 gfx::RectF DirectRenderer::QuadVertexRect() {
68 return gfx::RectF(-0.5f, -0.5f, 1.f, 1.f);
71 // static
72 void DirectRenderer::QuadRectTransform(gfx::Transform* quad_rect_transform,
73 const gfx::Transform& quad_transform,
74 const gfx::RectF& quad_rect) {
75 *quad_rect_transform = quad_transform;
76 quad_rect_transform->Translate(0.5 * quad_rect.width() + quad_rect.x(),
77 0.5 * quad_rect.height() + quad_rect.y());
78 quad_rect_transform->Scale(quad_rect.width(), quad_rect.height());
81 void DirectRenderer::InitializeViewport(DrawingFrame* frame,
82 gfx::Rect draw_rect,
83 gfx::Rect viewport_rect,
84 gfx::Size surface_size) {
85 bool flip_y = FlippedFramebuffer();
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 if (flip_y) {
92 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
93 draw_rect.right(),
94 draw_rect.bottom(),
95 draw_rect.y());
96 } else {
97 frame->projection_matrix = OrthoProjectionMatrix(draw_rect.x(),
98 draw_rect.right(),
99 draw_rect.y(),
100 draw_rect.bottom());
103 gfx::Rect window_rect = viewport_rect;
104 if (flip_y)
105 window_rect.set_y(surface_size.height() - viewport_rect.bottom());
106 frame->window_matrix = window_matrix(window_rect.x(),
107 window_rect.y(),
108 window_rect.width(),
109 window_rect.height());
110 SetDrawViewport(window_rect);
112 current_draw_rect_ = draw_rect;
113 current_viewport_rect_ = viewport_rect;
114 current_surface_size_ = surface_size;
117 gfx::Rect DirectRenderer::MoveFromDrawToWindowSpace(
118 const gfx::RectF& draw_rect) const {
119 gfx::Rect window_rect = gfx::ToEnclosingRect(draw_rect);
120 window_rect -= current_draw_rect_.OffsetFromOrigin();
121 window_rect += current_viewport_rect_.OffsetFromOrigin();
122 if (FlippedFramebuffer())
123 window_rect.set_y(current_surface_size_.height() - window_rect.bottom());
124 return window_rect;
127 DirectRenderer::DirectRenderer(RendererClient* client,
128 OutputSurface* output_surface,
129 ResourceProvider* resource_provider)
130 : Renderer(client),
131 output_surface_(output_surface),
132 resource_provider_(resource_provider) {}
134 DirectRenderer::~DirectRenderer() {}
136 bool DirectRenderer::CanReadPixels() const { return true; }
138 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
139 gfx::Vector2d amount) {
140 enlarge_pass_texture_amount_ = amount;
143 void DirectRenderer::DecideRenderPassAllocationsForFrame(
144 const RenderPassList& render_passes_in_draw_order) {
145 if (!resource_provider_)
146 return;
148 base::hash_map<RenderPass::Id, const RenderPass*> render_passes_in_frame;
149 for (size_t i = 0; i < render_passes_in_draw_order.size(); ++i)
150 render_passes_in_frame.insert(std::pair<RenderPass::Id, const RenderPass*>(
151 render_passes_in_draw_order[i]->id, render_passes_in_draw_order[i]));
153 std::vector<RenderPass::Id> passes_to_delete;
154 ScopedPtrHashMap<RenderPass::Id, CachedResource>::const_iterator pass_iter;
155 for (pass_iter = render_pass_textures_.begin();
156 pass_iter != render_pass_textures_.end();
157 ++pass_iter) {
158 base::hash_map<RenderPass::Id, const RenderPass*>::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 const RenderPass* render_pass_in_frame = it->second;
166 gfx::Size required_size = RenderPassTextureSize(render_pass_in_frame);
167 GLenum required_format = RenderPassTextureFormat(render_pass_in_frame);
168 CachedResource* texture = pass_iter->second;
169 DCHECK(texture);
171 bool size_appropriate = texture->size().width() >= required_size.width() &&
172 texture->size().height() >= required_size.height();
173 if (texture->id() &&
174 (!size_appropriate || texture->format() != required_format))
175 texture->Free();
178 // Delete RenderPass textures from the previous frame that will not be used
179 // again.
180 for (size_t i = 0; i < passes_to_delete.size(); ++i)
181 render_pass_textures_.erase(passes_to_delete[i]);
183 for (size_t i = 0; i < render_passes_in_draw_order.size(); ++i) {
184 if (!render_pass_textures_.contains(render_passes_in_draw_order[i]->id)) {
185 scoped_ptr<CachedResource> texture =
186 CachedResource::Create(resource_provider_);
187 render_pass_textures_.set(render_passes_in_draw_order[i]->id,
188 texture.Pass());
193 void DirectRenderer::DrawFrame(RenderPassList* render_passes_in_draw_order) {
194 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
195 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
196 render_passes_in_draw_order->size());
198 const RenderPass* root_render_pass = render_passes_in_draw_order->back();
199 DCHECK(root_render_pass);
201 DrawingFrame frame;
202 frame.root_render_pass = root_render_pass;
203 frame.root_damage_rect =
204 Capabilities().using_partial_swap && client_->AllowPartialSwap() ?
205 root_render_pass->damage_rect : root_render_pass->output_rect;
206 frame.root_damage_rect.Intersect(gfx::Rect(client_->DeviceViewport().size()));
208 EnsureBackbuffer();
210 // Only reshape when we know we are going to draw. Otherwise, the reshape
211 // can leave the window at the wrong size if we never draw and the proper
212 // viewport size is never set.
213 output_surface_->Reshape(client_->DeviceViewport().size(),
214 client_->DeviceScaleFactor());
216 BeginDrawingFrame(&frame);
217 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
218 RenderPass* pass = render_passes_in_draw_order->at(i);
219 DrawRenderPass(&frame, pass);
221 for (ScopedPtrVector<CopyOutputRequest>::iterator it =
222 pass->copy_requests.begin();
223 it != pass->copy_requests.end();
224 ++it) {
225 if (i > 0) {
226 // Doing a readback is destructive of our state on Mac, so make sure
227 // we restore the state between readbacks. http://crbug.com/99393.
228 UseRenderPass(&frame, pass);
230 CopyCurrentRenderPassToBitmap(&frame, pass->copy_requests.take(it));
233 FinishDrawingFrame(&frame);
235 render_passes_in_draw_order->clear();
238 gfx::RectF DirectRenderer::ComputeScissorRectForRenderPass(
239 const DrawingFrame* frame) {
240 gfx::RectF render_pass_scissor = frame->current_render_pass->output_rect;
242 if (frame->root_damage_rect == frame->root_render_pass->output_rect)
243 return render_pass_scissor;
245 gfx::Transform inverse_transform(gfx::Transform::kSkipInitialization);
246 if (frame->current_render_pass->transform_to_root_target.GetInverse(
247 &inverse_transform)) {
248 // Only intersect inverse-projected damage if the transform is invertible.
249 gfx::RectF damage_rect_in_render_pass_space =
250 MathUtil::ProjectClippedRect(inverse_transform,
251 frame->root_damage_rect);
252 render_pass_scissor.Intersect(damage_rect_in_render_pass_space);
255 return render_pass_scissor;
258 void DirectRenderer::SetScissorStateForQuad(const DrawingFrame* frame,
259 const DrawQuad& quad) {
260 if (quad.isClipped()) {
261 gfx::RectF quad_scissor_rect = quad.clipRect();
262 SetScissorTestRect(MoveFromDrawToWindowSpace(quad_scissor_rect));
263 } else {
264 EnsureScissorTestDisabled();
268 void DirectRenderer::SetScissorStateForQuadWithRenderPassScissor(
269 const DrawingFrame* frame,
270 const DrawQuad& quad,
271 const gfx::RectF& render_pass_scissor,
272 bool* should_skip_quad) {
273 gfx::RectF quad_scissor_rect = render_pass_scissor;
275 if (quad.isClipped())
276 quad_scissor_rect.Intersect(quad.clipRect());
278 if (quad_scissor_rect.IsEmpty()) {
279 *should_skip_quad = true;
280 return;
283 *should_skip_quad = false;
284 SetScissorTestRect(MoveFromDrawToWindowSpace(quad_scissor_rect));
287 void DirectRenderer::FinishDrawingQuadList() {}
289 void DirectRenderer::DrawRenderPass(DrawingFrame* frame,
290 const RenderPass* render_pass) {
291 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
292 if (!UseRenderPass(frame, render_pass))
293 return;
295 bool using_scissor_as_optimization =
296 Capabilities().using_partial_swap && client_->AllowPartialSwap();
297 gfx::RectF render_pass_scissor;
299 if (using_scissor_as_optimization) {
300 render_pass_scissor = ComputeScissorRectForRenderPass(frame);
301 SetScissorTestRect(MoveFromDrawToWindowSpace(render_pass_scissor));
304 if (frame->current_render_pass != frame->root_render_pass ||
305 client_->ShouldClearRootRenderPass()) {
306 if (!using_scissor_as_optimization)
307 EnsureScissorTestDisabled();
308 ClearFramebuffer(frame);
311 const QuadList& quad_list = render_pass->quad_list;
312 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
313 it != quad_list.BackToFrontEnd();
314 ++it) {
315 const DrawQuad& quad = *(*it);
316 bool should_skip_quad = false;
318 if (using_scissor_as_optimization) {
319 SetScissorStateForQuadWithRenderPassScissor(
320 frame, quad, render_pass_scissor, &should_skip_quad);
321 } else {
322 SetScissorStateForQuad(frame, quad);
325 if (!should_skip_quad)
326 DoDrawQuad(frame, *it);
328 FinishDrawingQuadList();
330 CachedResource* texture = render_pass_textures_.get(render_pass->id);
331 if (texture) {
332 texture->set_is_complete(
333 !render_pass->has_occlusion_from_outside_target_surface);
337 bool DirectRenderer::UseRenderPass(DrawingFrame* frame,
338 const RenderPass* render_pass) {
339 frame->current_render_pass = render_pass;
340 frame->current_texture = NULL;
342 if (render_pass == frame->root_render_pass) {
343 BindFramebufferToOutputSurface(frame);
344 InitializeViewport(frame,
345 render_pass->output_rect,
346 client_->DeviceViewport(),
347 output_surface_->SurfaceSize());
348 return true;
351 if (!resource_provider_)
352 return false;
354 CachedResource* texture = render_pass_textures_.get(render_pass->id);
355 DCHECK(texture);
357 gfx::Size size = RenderPassTextureSize(render_pass);
358 size.Enlarge(enlarge_pass_texture_amount_.x(),
359 enlarge_pass_texture_amount_.y());
360 if (!texture->id() &&
361 !texture->Allocate(size,
362 RenderPassTextureFormat(render_pass),
363 ResourceProvider::TextureUsageFramebuffer))
364 return false;
366 return BindFramebufferToTexture(frame, texture, render_pass->output_rect);
369 bool DirectRenderer::HaveCachedResourcesForRenderPassId(RenderPass::Id id)
370 const {
371 if (!Settings().cache_render_pass_contents)
372 return false;
374 CachedResource* texture = render_pass_textures_.get(id);
375 return texture && texture->id() && texture->is_complete();
378 // static
379 gfx::Size DirectRenderer::RenderPassTextureSize(const RenderPass* render_pass) {
380 return render_pass->output_rect.size();
383 // static
384 GLenum DirectRenderer::RenderPassTextureFormat(const RenderPass* render_pass) {
385 return GL_RGBA;
388 } // namespace cc