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/copy_output_request.h"
16 #include "cc/quads/draw_quad.h"
17 #include "ui/gfx/geometry/rect_conversions.h"
18 #include "ui/gfx/transform.h"
20 static gfx::Transform
OrthoProjectionMatrix(float left
,
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
;
29 if (!delta_x
|| !delta_y
)
31 proj
.matrix().set(0, 0, 2.0f
/ delta_x
);
32 proj
.matrix().set(0, 3, -(right
+ left
) / delta_x
);
33 proj
.matrix().set(1, 1, 2.0f
/ delta_y
);
34 proj
.matrix().set(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().set(2, 2, 0);
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);
59 DirectRenderer::DrawingFrame::DrawingFrame()
60 : root_render_pass(NULL
), current_render_pass(NULL
), current_texture(NULL
) {
63 DirectRenderer::DrawingFrame::~DrawingFrame() {}
67 gfx::RectF
DirectRenderer::QuadVertexRect() {
68 return gfx::RectF(-0.5f
, -0.5f
, 1.f
, 1.f
);
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 const gfx::Rect
& draw_rect
,
83 const gfx::Rect
& viewport_rect
,
84 const gfx::Size
& surface_size
) {
85 DCHECK_GE(viewport_rect
.x(), 0);
86 DCHECK_GE(viewport_rect
.y(), 0);
87 DCHECK_LE(viewport_rect
.right(), surface_size
.width());
88 DCHECK_LE(viewport_rect
.bottom(), surface_size
.height());
89 bool flip_y
= FlippedFramebuffer(frame
);
91 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
96 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
102 gfx::Rect window_rect
= viewport_rect
;
104 window_rect
.set_y(surface_size
.height() - viewport_rect
.bottom());
105 frame
->window_matrix
= window_matrix(window_rect
.x(),
108 window_rect
.height());
109 SetDrawViewport(window_rect
);
111 current_draw_rect_
= draw_rect
;
112 current_viewport_rect_
= viewport_rect
;
113 current_surface_size_
= surface_size
;
116 gfx::Rect
DirectRenderer::MoveFromDrawToWindowSpace(
117 const DrawingFrame
* frame
,
118 const gfx::Rect
& draw_rect
) const {
119 gfx::Rect window_rect
= draw_rect
;
120 window_rect
-= current_draw_rect_
.OffsetFromOrigin();
121 window_rect
+= current_viewport_rect_
.OffsetFromOrigin();
122 if (FlippedFramebuffer(frame
))
123 window_rect
.set_y(current_surface_size_
.height() - window_rect
.bottom());
127 DirectRenderer::DirectRenderer(RendererClient
* client
,
128 const RendererSettings
* settings
,
129 OutputSurface
* output_surface
,
130 ResourceProvider
* resource_provider
)
131 : Renderer(client
, settings
),
132 output_surface_(output_surface
),
133 resource_provider_(resource_provider
),
135 new OverlayProcessor(output_surface
, resource_provider
)) {
136 overlay_processor_
->Initialize();
139 DirectRenderer::~DirectRenderer() {}
141 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
142 const gfx::Vector2d
& amount
) {
143 enlarge_pass_texture_amount_
= amount
;
146 void DirectRenderer::DecideRenderPassAllocationsForFrame(
147 const RenderPassList
& render_passes_in_draw_order
) {
148 if (!resource_provider_
)
151 base::hash_map
<RenderPassId
, gfx::Size
> render_passes_in_frame
;
152 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
)
153 render_passes_in_frame
.insert(std::pair
<RenderPassId
, gfx::Size
>(
154 render_passes_in_draw_order
[i
]->id
,
155 RenderPassTextureSize(render_passes_in_draw_order
[i
])));
157 std::vector
<RenderPassId
> passes_to_delete
;
158 base::ScopedPtrHashMap
<RenderPassId
, ScopedResource
>::const_iterator
160 for (pass_iter
= render_pass_textures_
.begin();
161 pass_iter
!= render_pass_textures_
.end();
163 base::hash_map
<RenderPassId
, gfx::Size
>::const_iterator it
=
164 render_passes_in_frame
.find(pass_iter
->first
);
165 if (it
== render_passes_in_frame
.end()) {
166 passes_to_delete
.push_back(pass_iter
->first
);
170 gfx::Size required_size
= it
->second
;
171 ScopedResource
* texture
= pass_iter
->second
;
174 bool size_appropriate
= texture
->size().width() >= required_size
.width() &&
175 texture
->size().height() >= required_size
.height();
176 if (texture
->id() && !size_appropriate
)
180 // Delete RenderPass textures from the previous frame that will not be used
182 for (size_t i
= 0; i
< passes_to_delete
.size(); ++i
)
183 render_pass_textures_
.erase(passes_to_delete
[i
]);
185 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
) {
186 if (!render_pass_textures_
.contains(render_passes_in_draw_order
[i
]->id
)) {
187 scoped_ptr
<ScopedResource
> texture
=
188 ScopedResource::Create(resource_provider_
);
189 render_pass_textures_
.set(render_passes_in_draw_order
[i
]->id
,
195 void DirectRenderer::DrawFrame(RenderPassList
* render_passes_in_draw_order
,
196 float device_scale_factor
,
197 const gfx::Rect
& device_viewport_rect
,
198 const gfx::Rect
& device_clip_rect
,
199 bool disable_picture_quad_image_filtering
) {
200 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
201 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
202 render_passes_in_draw_order
->size());
204 const RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
205 DCHECK(root_render_pass
);
208 frame
.render_passes_in_draw_order
= render_passes_in_draw_order
;
209 frame
.root_render_pass
= root_render_pass
;
210 frame
.root_damage_rect
= Capabilities().using_partial_swap
211 ? root_render_pass
->damage_rect
212 : root_render_pass
->output_rect
;
213 frame
.root_damage_rect
.Intersect(gfx::Rect(device_viewport_rect
.size()));
214 frame
.device_viewport_rect
= device_viewport_rect
;
215 frame
.device_clip_rect
= device_clip_rect
;
216 frame
.disable_picture_quad_image_filtering
=
217 disable_picture_quad_image_filtering
;
219 overlay_processor_
->ProcessForOverlays(render_passes_in_draw_order
,
220 &frame
.overlay_list
);
224 // Only reshape when we know we are going to draw. Otherwise, the reshape
225 // can leave the window at the wrong size if we never draw and the proper
226 // viewport size is never set.
227 output_surface_
->Reshape(device_viewport_rect
.size(), device_scale_factor
);
229 BeginDrawingFrame(&frame
);
230 for (size_t i
= 0; i
< render_passes_in_draw_order
->size(); ++i
) {
231 RenderPass
* pass
= render_passes_in_draw_order
->at(i
);
232 DrawRenderPass(&frame
, pass
);
234 for (ScopedPtrVector
<CopyOutputRequest
>::iterator it
=
235 pass
->copy_requests
.begin();
236 it
!= pass
->copy_requests
.end();
239 // Doing a readback is destructive of our state on Mac, so make sure
240 // we restore the state between readbacks. http://crbug.com/99393.
241 UseRenderPass(&frame
, pass
);
243 CopyCurrentRenderPassToBitmap(&frame
, pass
->copy_requests
.take(it
));
246 FinishDrawingFrame(&frame
);
248 render_passes_in_draw_order
->clear();
251 gfx::Rect
DirectRenderer::ComputeScissorRectForRenderPass(
252 const DrawingFrame
* frame
) {
253 gfx::Rect render_pass_scissor
= frame
->current_render_pass
->output_rect
;
255 if (frame
->root_damage_rect
== frame
->root_render_pass
->output_rect
||
256 !frame
->current_render_pass
->copy_requests
.empty())
257 return render_pass_scissor
;
259 gfx::Transform
inverse_transform(gfx::Transform::kSkipInitialization
);
260 if (frame
->current_render_pass
->transform_to_root_target
.GetInverse(
261 &inverse_transform
)) {
262 // Only intersect inverse-projected damage if the transform is invertible.
263 gfx::Rect damage_rect_in_render_pass_space
=
264 MathUtil::ProjectEnclosingClippedRect(inverse_transform
,
265 frame
->root_damage_rect
);
266 render_pass_scissor
.Intersect(damage_rect_in_render_pass_space
);
269 return render_pass_scissor
;
272 bool DirectRenderer::NeedDeviceClip(const DrawingFrame
* frame
) const {
273 if (frame
->current_render_pass
!= frame
->root_render_pass
)
276 return !frame
->device_clip_rect
.Contains(frame
->device_viewport_rect
);
279 gfx::Rect
DirectRenderer::DeviceClipRectInWindowSpace(const DrawingFrame
* frame
)
281 gfx::Rect device_clip_rect
= frame
->device_clip_rect
;
282 if (FlippedFramebuffer(frame
))
283 device_clip_rect
.set_y(current_surface_size_
.height() -
284 device_clip_rect
.bottom());
285 return device_clip_rect
;
288 void DirectRenderer::SetScissorStateForQuad(const DrawingFrame
* frame
,
289 const DrawQuad
& quad
) {
290 if (quad
.isClipped()) {
291 SetScissorTestRectInDrawSpace(frame
, quad
.clipRect());
294 if (NeedDeviceClip(frame
)) {
295 SetScissorTestRect(DeviceClipRectInWindowSpace(frame
));
299 EnsureScissorTestDisabled();
302 void DirectRenderer::SetScissorStateForQuadWithRenderPassScissor(
303 const DrawingFrame
* frame
,
304 const DrawQuad
& quad
,
305 const gfx::Rect
& render_pass_scissor
,
306 bool* should_skip_quad
) {
307 gfx::Rect quad_scissor_rect
= render_pass_scissor
;
309 if (quad
.isClipped())
310 quad_scissor_rect
.Intersect(quad
.clipRect());
312 if (quad_scissor_rect
.IsEmpty()) {
313 *should_skip_quad
= true;
317 *should_skip_quad
= false;
318 SetScissorTestRectInDrawSpace(frame
, quad_scissor_rect
);
321 void DirectRenderer::SetScissorTestRectInDrawSpace(
322 const DrawingFrame
* frame
,
323 const gfx::Rect
& draw_space_rect
) {
324 gfx::Rect window_space_rect
=
325 MoveFromDrawToWindowSpace(frame
, draw_space_rect
);
326 if (NeedDeviceClip(frame
))
327 window_space_rect
.Intersect(DeviceClipRectInWindowSpace(frame
));
328 SetScissorTestRect(window_space_rect
);
331 void DirectRenderer::FinishDrawingQuadList() {}
333 void DirectRenderer::DrawRenderPass(DrawingFrame
* frame
,
334 const RenderPass
* render_pass
) {
335 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
336 if (!UseRenderPass(frame
, render_pass
))
339 bool using_scissor_as_optimization
= Capabilities().using_partial_swap
;
340 gfx::Rect render_pass_scissor
;
341 bool draw_rect_covers_full_surface
= true;
342 if (frame
->current_render_pass
== frame
->root_render_pass
&&
343 !frame
->device_viewport_rect
.Contains(
344 gfx::Rect(output_surface_
->SurfaceSize())))
345 draw_rect_covers_full_surface
= false;
347 if (using_scissor_as_optimization
) {
348 render_pass_scissor
= ComputeScissorRectForRenderPass(frame
);
349 SetScissorTestRectInDrawSpace(frame
, render_pass_scissor
);
350 if (!render_pass_scissor
.Contains(frame
->current_render_pass
->output_rect
))
351 draw_rect_covers_full_surface
= false;
354 if (frame
->current_render_pass
!= frame
->root_render_pass
||
355 settings_
->should_clear_root_render_pass
) {
356 if (NeedDeviceClip(frame
)) {
357 SetScissorTestRect(DeviceClipRectInWindowSpace(frame
));
358 draw_rect_covers_full_surface
= false;
359 } else if (!using_scissor_as_optimization
) {
360 EnsureScissorTestDisabled();
363 bool has_external_stencil_test
=
364 output_surface_
->HasExternalStencilTest() &&
365 frame
->current_render_pass
== frame
->root_render_pass
;
367 DiscardPixels(has_external_stencil_test
, draw_rect_covers_full_surface
);
368 ClearFramebuffer(frame
, has_external_stencil_test
);
371 const QuadList
& quad_list
= render_pass
->quad_list
;
372 for (auto it
= quad_list
.BackToFrontBegin(); it
!= quad_list
.BackToFrontEnd();
374 const DrawQuad
& quad
= **it
;
375 bool should_skip_quad
= false;
377 if (using_scissor_as_optimization
) {
378 SetScissorStateForQuadWithRenderPassScissor(
379 frame
, quad
, render_pass_scissor
, &should_skip_quad
);
381 SetScissorStateForQuad(frame
, quad
);
384 if (!should_skip_quad
)
385 DoDrawQuad(frame
, &quad
);
387 FinishDrawingQuadList();
390 bool DirectRenderer::UseRenderPass(DrawingFrame
* frame
,
391 const RenderPass
* render_pass
) {
392 frame
->current_render_pass
= render_pass
;
393 frame
->current_texture
= NULL
;
395 if (render_pass
== frame
->root_render_pass
) {
396 BindFramebufferToOutputSurface(frame
);
397 InitializeViewport(frame
,
398 render_pass
->output_rect
,
399 frame
->device_viewport_rect
,
400 output_surface_
->SurfaceSize());
404 ScopedResource
* texture
= render_pass_textures_
.get(render_pass
->id
);
407 gfx::Size size
= RenderPassTextureSize(render_pass
);
408 size
.Enlarge(enlarge_pass_texture_amount_
.x(),
409 enlarge_pass_texture_amount_
.y());
412 size
, ResourceProvider::TextureHintImmutableFramebuffer
, RGBA_8888
);
413 DCHECK(texture
->id());
415 return BindFramebufferToTexture(frame
, texture
, render_pass
->output_rect
);
418 bool DirectRenderer::HasAllocatedResourcesForTesting(RenderPassId id
) const {
419 ScopedResource
* texture
= render_pass_textures_
.get(id
);
420 return texture
&& texture
->id();
424 gfx::Size
DirectRenderer::RenderPassTextureSize(const RenderPass
* render_pass
) {
425 return render_pass
->output_rect
.size();