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/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 "cc/resources/raster_worker_pool.h"
18 #include "ui/gfx/rect_conversions.h"
19 #include "ui/gfx/transform.h"
21 static gfx::Transform
OrthoProjectionMatrix(float left
,
25 // Use the standard formula to map the clipping frustum to the cube from
26 // [-1, -1, -1] to [1, 1, 1].
27 float delta_x
= right
- left
;
28 float delta_y
= top
- bottom
;
30 if (!delta_x
|| !delta_y
)
32 proj
.matrix().set(0, 0, 2.0f
/ delta_x
);
33 proj
.matrix().set(0, 3, -(right
+ left
) / delta_x
);
34 proj
.matrix().set(1, 1, 2.0f
/ delta_y
);
35 proj
.matrix().set(1, 3, -(top
+ bottom
) / delta_y
);
37 // Z component of vertices is always set to zero as we don't use the depth
38 // buffer while drawing.
39 proj
.matrix().set(2, 2, 0);
44 static gfx::Transform
window_matrix(int x
, int y
, int width
, int height
) {
45 gfx::Transform canvas
;
47 // Map to window position and scale up to pixel coordinates.
48 canvas
.Translate3d(x
, y
, 0);
49 canvas
.Scale3d(width
, height
, 0);
51 // Map from ([-1, -1] to [1, 1]) -> ([0, 0] to [1, 1])
52 canvas
.Translate3d(0.5, 0.5, 0.5);
53 canvas
.Scale3d(0.5, 0.5, 0.5);
60 DirectRenderer::DrawingFrame::DrawingFrame()
61 : root_render_pass(NULL
), current_render_pass(NULL
), current_texture(NULL
) {
64 DirectRenderer::DrawingFrame::~DrawingFrame() {}
68 gfx::RectF
DirectRenderer::QuadVertexRect() {
69 return gfx::RectF(-0.5f
, -0.5f
, 1.f
, 1.f
);
73 void DirectRenderer::QuadRectTransform(gfx::Transform
* quad_rect_transform
,
74 const gfx::Transform
& quad_transform
,
75 const gfx::RectF
& quad_rect
) {
76 *quad_rect_transform
= quad_transform
;
77 quad_rect_transform
->Translate(0.5 * quad_rect
.width() + quad_rect
.x(),
78 0.5 * quad_rect
.height() + quad_rect
.y());
79 quad_rect_transform
->Scale(quad_rect
.width(), quad_rect
.height());
82 void DirectRenderer::InitializeViewport(DrawingFrame
* frame
,
83 const gfx::Rect
& draw_rect
,
84 const gfx::Rect
& viewport_rect
,
85 const gfx::Size
& surface_size
) {
86 bool flip_y
= FlippedFramebuffer();
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());
93 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
98 frame
->projection_matrix
= OrthoProjectionMatrix(draw_rect
.x(),
104 gfx::Rect window_rect
= viewport_rect
;
106 window_rect
.set_y(surface_size
.height() - viewport_rect
.bottom());
107 frame
->window_matrix
= window_matrix(window_rect
.x(),
110 window_rect
.height());
111 SetDrawViewport(window_rect
);
113 current_draw_rect_
= draw_rect
;
114 current_viewport_rect_
= viewport_rect
;
115 current_surface_size_
= surface_size
;
118 gfx::Rect
DirectRenderer::MoveFromDrawToWindowSpace(
119 const gfx::Rect
& draw_rect
) const {
120 gfx::Rect window_rect
= draw_rect
;
121 window_rect
-= current_draw_rect_
.OffsetFromOrigin();
122 window_rect
+= current_viewport_rect_
.OffsetFromOrigin();
123 if (FlippedFramebuffer())
124 window_rect
.set_y(current_surface_size_
.height() - window_rect
.bottom());
128 DirectRenderer::DirectRenderer(RendererClient
* client
,
129 const LayerTreeSettings
* settings
,
130 OutputSurface
* output_surface
,
131 ResourceProvider
* resource_provider
)
132 : Renderer(client
, settings
),
133 output_surface_(output_surface
),
134 resource_provider_(resource_provider
),
136 new OverlayProcessor(output_surface
, resource_provider
)) {
137 overlay_processor_
->Initialize();
140 DirectRenderer::~DirectRenderer() {}
142 bool DirectRenderer::CanReadPixels() const { return true; }
144 void DirectRenderer::SetEnlargePassTextureAmountForTesting(
145 const gfx::Vector2d
& amount
) {
146 enlarge_pass_texture_amount_
= amount
;
149 void DirectRenderer::DecideRenderPassAllocationsForFrame(
150 const RenderPassList
& render_passes_in_draw_order
) {
151 if (!resource_provider_
)
154 base::hash_map
<RenderPass::Id
, gfx::Size
> render_passes_in_frame
;
155 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
)
156 render_passes_in_frame
.insert(std::pair
<RenderPass::Id
, gfx::Size
>(
157 render_passes_in_draw_order
[i
]->id
,
158 RenderPassTextureSize(render_passes_in_draw_order
[i
])));
160 std::vector
<RenderPass::Id
> passes_to_delete
;
161 base::ScopedPtrHashMap
<RenderPass::Id
, ScopedResource
>::const_iterator
163 for (pass_iter
= render_pass_textures_
.begin();
164 pass_iter
!= render_pass_textures_
.end();
166 base::hash_map
<RenderPass::Id
, gfx::Size
>::const_iterator it
=
167 render_passes_in_frame
.find(pass_iter
->first
);
168 if (it
== render_passes_in_frame
.end()) {
169 passes_to_delete
.push_back(pass_iter
->first
);
173 gfx::Size required_size
= it
->second
;
174 ScopedResource
* texture
= pass_iter
->second
;
177 bool size_appropriate
= texture
->size().width() >= required_size
.width() &&
178 texture
->size().height() >= required_size
.height();
179 if (texture
->id() && !size_appropriate
)
183 // Delete RenderPass textures from the previous frame that will not be used
185 for (size_t i
= 0; i
< passes_to_delete
.size(); ++i
)
186 render_pass_textures_
.erase(passes_to_delete
[i
]);
188 for (size_t i
= 0; i
< render_passes_in_draw_order
.size(); ++i
) {
189 if (!render_pass_textures_
.contains(render_passes_in_draw_order
[i
]->id
)) {
190 scoped_ptr
<ScopedResource
> texture
=
191 ScopedResource::Create(resource_provider_
);
192 render_pass_textures_
.set(render_passes_in_draw_order
[i
]->id
,
198 void DirectRenderer::DrawFrame(RenderPassList
* render_passes_in_draw_order
,
199 float device_scale_factor
,
200 const gfx::Rect
& device_viewport_rect
,
201 const gfx::Rect
& device_clip_rect
,
202 bool disable_picture_quad_image_filtering
) {
203 TRACE_EVENT0("cc", "DirectRenderer::DrawFrame");
204 UMA_HISTOGRAM_COUNTS("Renderer4.renderPassCount",
205 render_passes_in_draw_order
->size());
207 const RenderPass
* root_render_pass
= render_passes_in_draw_order
->back();
208 DCHECK(root_render_pass
);
211 frame
.root_render_pass
= root_render_pass
;
212 frame
.root_damage_rect
= Capabilities().using_partial_swap
213 ? root_render_pass
->damage_rect
214 : root_render_pass
->output_rect
;
215 frame
.root_damage_rect
.Intersect(gfx::Rect(device_viewport_rect
.size()));
216 frame
.device_viewport_rect
= device_viewport_rect
;
217 frame
.device_clip_rect
= device_clip_rect
;
218 frame
.disable_picture_quad_image_filtering
=
219 disable_picture_quad_image_filtering
;
221 overlay_processor_
->ProcessForOverlays(render_passes_in_draw_order
,
222 &frame
.overlay_list
);
226 // Only reshape when we know we are going to draw. Otherwise, the reshape
227 // can leave the window at the wrong size if we never draw and the proper
228 // viewport size is never set.
229 output_surface_
->Reshape(device_viewport_rect
.size(), device_scale_factor
);
231 BeginDrawingFrame(&frame
);
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();
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
)
278 return !frame
->device_clip_rect
.Contains(frame
->device_viewport_rect
);
281 gfx::Rect
DirectRenderer::DeviceClipRectInWindowSpace(const DrawingFrame
* frame
)
283 gfx::Rect device_clip_rect
= frame
->device_clip_rect
;
284 if (FlippedFramebuffer())
285 device_clip_rect
.set_y(current_surface_size_
.height() -
286 device_clip_rect
.bottom());
287 return device_clip_rect
;
290 void DirectRenderer::SetScissorStateForQuad(const DrawingFrame
* frame
,
291 const DrawQuad
& quad
) {
292 if (quad
.isClipped()) {
293 SetScissorTestRectInDrawSpace(frame
, quad
.clipRect());
296 if (NeedDeviceClip(frame
)) {
297 SetScissorTestRect(DeviceClipRectInWindowSpace(frame
));
301 EnsureScissorTestDisabled();
304 void DirectRenderer::SetScissorStateForQuadWithRenderPassScissor(
305 const DrawingFrame
* frame
,
306 const DrawQuad
& quad
,
307 const gfx::Rect
& render_pass_scissor
,
308 bool* should_skip_quad
) {
309 gfx::Rect quad_scissor_rect
= render_pass_scissor
;
311 if (quad
.isClipped())
312 quad_scissor_rect
.Intersect(quad
.clipRect());
314 if (quad_scissor_rect
.IsEmpty()) {
315 *should_skip_quad
= true;
319 *should_skip_quad
= false;
320 SetScissorTestRectInDrawSpace(frame
, quad_scissor_rect
);
323 void DirectRenderer::SetScissorTestRectInDrawSpace(
324 const DrawingFrame
* frame
,
325 const gfx::Rect
& draw_space_rect
) {
326 gfx::Rect window_space_rect
= MoveFromDrawToWindowSpace(draw_space_rect
);
327 if (NeedDeviceClip(frame
))
328 window_space_rect
.Intersect(DeviceClipRectInWindowSpace(frame
));
329 SetScissorTestRect(window_space_rect
);
332 void DirectRenderer::FinishDrawingQuadList() {}
334 void DirectRenderer::DrawRenderPass(DrawingFrame
* frame
,
335 const RenderPass
* render_pass
) {
336 TRACE_EVENT0("cc", "DirectRenderer::DrawRenderPass");
337 if (!UseRenderPass(frame
, render_pass
))
340 bool using_scissor_as_optimization
= Capabilities().using_partial_swap
;
341 gfx::Rect render_pass_scissor
;
342 bool draw_rect_covers_full_surface
= true;
343 if (frame
->current_render_pass
== frame
->root_render_pass
&&
344 !frame
->device_viewport_rect
.Contains(
345 gfx::Rect(output_surface_
->SurfaceSize())))
346 draw_rect_covers_full_surface
= false;
348 if (using_scissor_as_optimization
) {
349 render_pass_scissor
= ComputeScissorRectForRenderPass(frame
);
350 SetScissorTestRectInDrawSpace(frame
, render_pass_scissor
);
351 if (!render_pass_scissor
.Contains(frame
->current_render_pass
->output_rect
))
352 draw_rect_covers_full_surface
= false;
355 if (frame
->current_render_pass
!= frame
->root_render_pass
||
356 settings_
->should_clear_root_render_pass
) {
357 if (NeedDeviceClip(frame
)) {
358 SetScissorTestRect(DeviceClipRectInWindowSpace(frame
));
359 draw_rect_covers_full_surface
= false;
360 } else if (!using_scissor_as_optimization
) {
361 EnsureScissorTestDisabled();
364 bool has_external_stencil_test
=
365 output_surface_
->HasExternalStencilTest() &&
366 frame
->current_render_pass
== frame
->root_render_pass
;
368 DiscardPixels(has_external_stencil_test
, draw_rect_covers_full_surface
);
369 ClearFramebuffer(frame
, has_external_stencil_test
);
372 const QuadList
& quad_list
= render_pass
->quad_list
;
373 for (QuadList::ConstBackToFrontIterator it
= quad_list
.BackToFrontBegin();
374 it
!= quad_list
.BackToFrontEnd();
376 const DrawQuad
& quad
= *(*it
);
377 bool should_skip_quad
= false;
379 if (using_scissor_as_optimization
) {
380 SetScissorStateForQuadWithRenderPassScissor(
381 frame
, quad
, render_pass_scissor
, &should_skip_quad
);
383 SetScissorStateForQuad(frame
, quad
);
386 if (!should_skip_quad
)
387 DoDrawQuad(frame
, *it
);
389 FinishDrawingQuadList();
392 bool DirectRenderer::UseRenderPass(DrawingFrame
* frame
,
393 const RenderPass
* render_pass
) {
394 frame
->current_render_pass
= render_pass
;
395 frame
->current_texture
= NULL
;
397 if (render_pass
== frame
->root_render_pass
) {
398 BindFramebufferToOutputSurface(frame
);
399 InitializeViewport(frame
,
400 render_pass
->output_rect
,
401 frame
->device_viewport_rect
,
402 output_surface_
->SurfaceSize());
406 ScopedResource
* texture
= render_pass_textures_
.get(render_pass
->id
);
409 gfx::Size size
= RenderPassTextureSize(render_pass
);
410 size
.Enlarge(enlarge_pass_texture_amount_
.x(),
411 enlarge_pass_texture_amount_
.y());
414 size
, ResourceProvider::TextureUsageFramebuffer
, RGBA_8888
);
415 DCHECK(texture
->id());
417 return BindFramebufferToTexture(frame
, texture
, render_pass
->output_rect
);
420 void DirectRenderer::RunOnDemandRasterTask(Task
* on_demand_raster_task
) {
421 TaskGraphRunner
* task_graph_runner
= RasterWorkerPool::GetTaskGraphRunner();
422 DCHECK(task_graph_runner
);
424 // Make sure we have a unique task namespace token.
425 if (!on_demand_task_namespace_
.IsValid())
426 on_demand_task_namespace_
= task_graph_runner
->GetNamespaceToken();
428 // Construct a task graph that contains this single raster task.
430 graph
.nodes
.push_back(
431 TaskGraph::Node(on_demand_raster_task
,
432 RasterWorkerPool::kOnDemandRasterTaskPriority
,
435 // Schedule task and wait for task graph runner to finish running it.
436 task_graph_runner
->ScheduleTasks(on_demand_task_namespace_
, &graph
);
437 task_graph_runner
->WaitForTasksToFinishRunning(on_demand_task_namespace_
);
439 // Collect task now that it has finished running.
440 Task::Vector completed_tasks
;
441 task_graph_runner
->CollectCompletedTasks(on_demand_task_namespace_
,
443 DCHECK_EQ(1u, completed_tasks
.size());
444 DCHECK_EQ(completed_tasks
[0], on_demand_raster_task
);
447 bool DirectRenderer::HasAllocatedResourcesForTesting(RenderPass::Id id
)
449 ScopedResource
* texture
= render_pass_textures_
.get(id
);
450 return texture
&& texture
->id();
454 gfx::Size
DirectRenderer::RenderPassTextureSize(const RenderPass
* render_pass
) {
455 return render_pass
->output_rect
.size();