cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / cc / output / direct_renderer.cc
blob87eefba4839a18ef563943645980cf9e158071d3
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/debug/trace_event.h"
13 #include "base/metrics/histogram.h"
14 #include "cc/base/math_util.h"
15 #include "cc/output/copy_output_request.h"
16 #include "cc/quads/draw_quad.h"
17 #include "ui/gfx/rect_conversions.h"
18 #include "ui/gfx/transform.h"
20 static gfx::Transform OrthoProjectionMatrix(float left,
21 float right,
22 float bottom,
23 float top) {
24 // Use the standard formula to map the clipping frustum to the cube from
25 // [-1, -1, -1] to [1, 1, 1].
26 float delta_x = right - left;
27 float delta_y = top - bottom;
28 gfx::Transform proj;
29 if (!delta_x || !delta_y)
30 return proj;
31 proj.matrix().setDouble(0, 0, 2.0f / delta_x);
32 proj.matrix().setDouble(0, 3, -(right + left) / delta_x);
33 proj.matrix().setDouble(1, 1, 2.0f / delta_y);
34 proj.matrix().setDouble(1, 3, -(top + bottom) / delta_y);
36 // Z component of vertices is always set to zero as we don't use the depth
37 // buffer while drawing.
38 proj.matrix().setDouble(2, 2, 0);
40 return proj;
43 static gfx::Transform window_matrix(int x, int y, int width, int height) {
44 gfx::Transform canvas;
46 // Map to window position and scale up to pixel coordinates.
47 canvas.Translate3d(x, y, 0);
48 canvas.Scale3d(width, height, 0);
50 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
51 canvas.Translate3d(0.5, 0.5, 0.5);
52 canvas.Scale3d(0.5, 0.5, 0.5);
54 return canvas;
57 namespace cc {
59 DirectRenderer::DrawingFrame::DrawingFrame()
60 : root_render_pass(NULL),
61 current_render_pass(NULL),
62 current_texture(NULL),
63 offscreen_context_provider(NULL) {}
65 DirectRenderer::DrawingFrame::~DrawingFrame() {}
68 // static
69 gfx::RectF DirectRenderer::QuadVertexRect() {
70 return gfx::RectF(-0.5f, -0.5f, 1.f, 1.f);
73 // static
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 gfx::Rect draw_rect,
85 gfx::Rect viewport_rect,
86 gfx::Size surface_size) {
87 bool flip_y = FlippedFramebuffer();
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 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 SetDrawViewport(window_rect);
114 current_draw_rect_ = draw_rect;
115 current_viewport_rect_ = viewport_rect;
116 current_surface_size_ = surface_size;
119 gfx::Rect DirectRenderer::MoveFromDrawToWindowSpace(
120 const gfx::RectF& draw_rect) const {
121 gfx::Rect window_rect = gfx::ToEnclosingRect(draw_rect);
122 window_rect -= current_draw_rect_.OffsetFromOrigin();
123 window_rect += current_viewport_rect_.OffsetFromOrigin();
124 if (FlippedFramebuffer())
125 window_rect.set_y(current_surface_size_.height() - window_rect.bottom());
126 return window_rect;
129 DirectRenderer::DirectRenderer(RendererClient* client,
130 OutputSurface* output_surface,
131 ResourceProvider* resource_provider)
132 : Renderer(client),
133 output_surface_(output_surface),
134 resource_provider_(resource_provider) {}
136 DirectRenderer::~DirectRenderer() {}
138 bool DirectRenderer::CanReadPixels() const { return true; }
140 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
141 gfx::Vector2d amount) {
142 enlarge_pass_texture_amount_ = amount;
145 void DirectRenderer::DecideRenderPassAllocationsForFrame(
146 const RenderPassList& render_passes_in_draw_order) {
147 if (!resource_provider_)
148 return;
150 base::hash_map<RenderPass::Id, const RenderPass*> 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<RenderPass::Id, const RenderPass*>(
153 render_passes_in_draw_order[i]->id, render_passes_in_draw_order[i]));
155 std::vector<RenderPass::Id> passes_to_delete;
156 base::ScopedPtrHashMap<RenderPass::Id, CachedResource>::const_iterator
157 pass_iter;
158 for (pass_iter = render_pass_textures_.begin();
159 pass_iter != render_pass_textures_.end();
160 ++pass_iter) {
161 base::hash_map<RenderPass::Id, const RenderPass*>::const_iterator it =
162 render_passes_in_frame.find(pass_iter->first);
163 if (it == render_passes_in_frame.end()) {
164 passes_to_delete.push_back(pass_iter->first);
165 continue;
168 const RenderPass* render_pass_in_frame = it->second;
169 gfx::Size required_size = RenderPassTextureSize(render_pass_in_frame);
170 GLenum required_format = RenderPassTextureFormat(render_pass_in_frame);
171 CachedResource* texture = pass_iter->second;
172 DCHECK(texture);
174 bool size_appropriate = texture->size().width() >= required_size.width() &&
175 texture->size().height() >= required_size.height();
176 if (texture->id() &&
177 (!size_appropriate || texture->format() != required_format))
178 texture->Free();
181 // Delete RenderPass textures from the previous frame that will not be used
182 // again.
183 for (size_t i = 0; i < passes_to_delete.size(); ++i)
184 render_pass_textures_.erase(passes_to_delete[i]);
186 for (size_t i = 0; i < render_passes_in_draw_order.size(); ++i) {
187 if (!render_pass_textures_.contains(render_passes_in_draw_order[i]->id)) {
188 scoped_ptr<CachedResource> texture =
189 CachedResource::Create(resource_provider_);
190 render_pass_textures_.set(render_passes_in_draw_order[i]->id,
191 texture.Pass());
196 void DirectRenderer::DrawFrame(RenderPassList* render_passes_in_draw_order,
197 ContextProvider* offscreen_context_provider) {
198 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
199 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
200 render_passes_in_draw_order->size());
202 const RenderPass* root_render_pass = render_passes_in_draw_order->back();
203 DCHECK(root_render_pass);
205 DrawingFrame frame;
206 frame.root_render_pass = root_render_pass;
207 frame.root_damage_rect =
208 Capabilities().using_partial_swap && client_->AllowPartialSwap() ?
209 root_render_pass->damage_rect : root_render_pass->output_rect;
210 frame.root_damage_rect.Intersect(gfx::Rect(client_->DeviceViewport().size()));
211 frame.offscreen_context_provider = offscreen_context_provider;
213 EnsureBackbuffer();
215 // Only reshape when we know we are going to draw. Otherwise, the reshape
216 // can leave the window at the wrong size if we never draw and the proper
217 // viewport size is never set.
218 output_surface_->Reshape(client_->DeviceViewport().size(),
219 client_->DeviceScaleFactor());
221 BeginDrawingFrame(&frame);
222 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
223 RenderPass* pass = render_passes_in_draw_order->at(i);
224 DrawRenderPass(&frame, pass);
226 for (ScopedPtrVector<CopyOutputRequest>::iterator it =
227 pass->copy_requests.begin();
228 it != pass->copy_requests.end();
229 ++it) {
230 if (i > 0) {
231 // Doing a readback is destructive of our state on Mac, so make sure
232 // we restore the state between readbacks. http://crbug.com/99393.
233 UseRenderPass(&frame, pass);
235 CopyCurrentRenderPassToBitmap(&frame, pass->copy_requests.take(it));
238 FinishDrawingFrame(&frame);
240 render_passes_in_draw_order->clear();
243 gfx::RectF DirectRenderer::ComputeScissorRectForRenderPass(
244 const DrawingFrame* frame) {
245 gfx::RectF render_pass_scissor = frame->current_render_pass->output_rect;
247 if (frame->root_damage_rect == frame->root_render_pass->output_rect)
248 return render_pass_scissor;
250 gfx::Transform inverse_transform(gfx::Transform::kSkipInitialization);
251 if (frame->current_render_pass->transform_to_root_target.GetInverse(
252 &inverse_transform)) {
253 // Only intersect inverse-projected damage if the transform is invertible.
254 gfx::RectF damage_rect_in_render_pass_space =
255 MathUtil::ProjectClippedRect(inverse_transform,
256 frame->root_damage_rect);
257 render_pass_scissor.Intersect(damage_rect_in_render_pass_space);
260 return render_pass_scissor;
263 void DirectRenderer::SetScissorStateForQuad(const DrawingFrame* frame,
264 const DrawQuad& quad) {
265 if (quad.isClipped()) {
266 gfx::RectF quad_scissor_rect = quad.clipRect();
267 SetScissorTestRect(MoveFromDrawToWindowSpace(quad_scissor_rect));
268 } else {
269 EnsureScissorTestDisabled();
273 void DirectRenderer::SetScissorStateForQuadWithRenderPassScissor(
274 const DrawingFrame* frame,
275 const DrawQuad& quad,
276 const gfx::RectF& render_pass_scissor,
277 bool* should_skip_quad) {
278 gfx::RectF quad_scissor_rect = render_pass_scissor;
280 if (quad.isClipped())
281 quad_scissor_rect.Intersect(quad.clipRect());
283 if (quad_scissor_rect.IsEmpty()) {
284 *should_skip_quad = true;
285 return;
288 *should_skip_quad = false;
289 SetScissorTestRect(MoveFromDrawToWindowSpace(quad_scissor_rect));
292 void DirectRenderer::FinishDrawingQuadList() {}
294 void DirectRenderer::DrawRenderPass(DrawingFrame* frame,
295 const RenderPass* render_pass) {
296 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
297 if (!UseRenderPass(frame, render_pass))
298 return;
300 bool using_scissor_as_optimization =
301 Capabilities().using_partial_swap && client_->AllowPartialSwap();
302 gfx::RectF render_pass_scissor;
304 if (using_scissor_as_optimization) {
305 render_pass_scissor = ComputeScissorRectForRenderPass(frame);
306 SetScissorTestRect(MoveFromDrawToWindowSpace(render_pass_scissor));
309 if (frame->current_render_pass != frame->root_render_pass ||
310 client_->ShouldClearRootRenderPass()) {
311 if (!using_scissor_as_optimization)
312 EnsureScissorTestDisabled();
313 ClearFramebuffer(frame);
316 const QuadList& quad_list = render_pass->quad_list;
317 for (QuadList::ConstBackToFrontIterator it = quad_list.BackToFrontBegin();
318 it != quad_list.BackToFrontEnd();
319 ++it) {
320 const DrawQuad& quad = *(*it);
321 bool should_skip_quad = false;
323 if (using_scissor_as_optimization) {
324 SetScissorStateForQuadWithRenderPassScissor(
325 frame, quad, render_pass_scissor, &should_skip_quad);
326 } else {
327 SetScissorStateForQuad(frame, quad);
330 if (!should_skip_quad)
331 DoDrawQuad(frame, *it);
333 FinishDrawingQuadList();
335 CachedResource* texture = render_pass_textures_.get(render_pass->id);
336 if (texture) {
337 texture->set_is_complete(
338 !render_pass->has_occlusion_from_outside_target_surface);
342 bool DirectRenderer::UseRenderPass(DrawingFrame* frame,
343 const RenderPass* render_pass) {
344 frame->current_render_pass = render_pass;
345 frame->current_texture = NULL;
347 if (render_pass == frame->root_render_pass) {
348 BindFramebufferToOutputSurface(frame);
349 InitializeViewport(frame,
350 render_pass->output_rect,
351 client_->DeviceViewport(),
352 output_surface_->SurfaceSize());
353 return true;
356 if (!resource_provider_)
357 return false;
359 CachedResource* texture = render_pass_textures_.get(render_pass->id);
360 DCHECK(texture);
362 gfx::Size size = RenderPassTextureSize(render_pass);
363 size.Enlarge(enlarge_pass_texture_amount_.x(),
364 enlarge_pass_texture_amount_.y());
365 if (!texture->id() &&
366 !texture->Allocate(size,
367 RenderPassTextureFormat(render_pass),
368 ResourceProvider::TextureUsageFramebuffer))
369 return false;
371 return BindFramebufferToTexture(frame, texture, render_pass->output_rect);
374 bool DirectRenderer::HaveCachedResourcesForRenderPassId(RenderPass::Id id)
375 const {
376 if (!Settings().cache_render_pass_contents)
377 return false;
379 CachedResource* texture = render_pass_textures_.get(id);
380 return texture && texture->id() && texture->is_complete();
383 // static
384 gfx::Size DirectRenderer::RenderPassTextureSize(const RenderPass* render_pass) {
385 return render_pass->output_rect.size();
388 // static
389 GLenum DirectRenderer::RenderPassTextureFormat(const RenderPass* render_pass) {
390 return GL_RGBA;
393 } // namespace cc