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/layers/delegated_renderer_layer_impl.h"
10 #include "base/bind.h"
11 #include "base/containers/hash_tables.h"
12 #include "cc/base/math_util.h"
13 #include "cc/layers/append_quads_data.h"
14 #include "cc/layers/render_pass_sink.h"
15 #include "cc/output/delegated_frame_data.h"
16 #include "cc/quads/render_pass_draw_quad.h"
17 #include "cc/quads/solid_color_draw_quad.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "cc/trees/occlusion_tracker.h"
23 DelegatedRendererLayerImpl::DelegatedRendererLayerImpl(LayerTreeImpl
* tree_impl
,
25 : LayerImpl(tree_impl
, id
),
26 have_render_passes_to_push_(false),
27 inverse_device_scale_factor_(1.0f
),
29 own_child_id_(false) {
32 DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
37 bool DelegatedRendererLayerImpl::HasDelegatedContent() const { return true; }
39 bool DelegatedRendererLayerImpl::HasContributingDelegatedRenderPasses() const {
40 // The root RenderPass for the layer is merged with its target
41 // RenderPass in each frame. So we only have extra RenderPasses
42 // to merge when we have a non-root RenderPass present.
43 return render_passes_in_draw_order_
.size() > 1;
46 static ResourceProvider::ResourceId
ResourceRemapHelper(
48 const ResourceProvider::ResourceIdMap
& child_to_parent_map
,
49 ResourceProvider::ResourceIdArray
* resources_in_frame
,
50 ResourceProvider::ResourceId id
) {
52 ResourceProvider::ResourceIdMap::const_iterator it
=
53 child_to_parent_map
.find(id
);
54 if (it
== child_to_parent_map
.end()) {
55 *invalid_frame
= true;
59 DCHECK_EQ(it
->first
, id
);
60 ResourceProvider::ResourceId remapped_id
= it
->second
;
61 resources_in_frame
->push_back(id
);
65 void DelegatedRendererLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
66 LayerImpl::PushPropertiesTo(layer
);
68 DelegatedRendererLayerImpl
* delegated_layer
=
69 static_cast<DelegatedRendererLayerImpl
*>(layer
);
71 // If we have a new child_id to give to the active layer, it should
72 // have already deleted its old child_id.
73 DCHECK(delegated_layer
->child_id_
== 0 ||
74 delegated_layer
->child_id_
== child_id_
);
75 delegated_layer
->inverse_device_scale_factor_
= inverse_device_scale_factor_
;
76 delegated_layer
->child_id_
= child_id_
;
77 delegated_layer
->own_child_id_
= true;
78 own_child_id_
= false;
80 if (have_render_passes_to_push_
) {
81 // This passes ownership of the render passes to the active tree.
82 delegated_layer
->SetRenderPasses(&render_passes_in_draw_order_
);
83 DCHECK(render_passes_in_draw_order_
.empty());
84 have_render_passes_to_push_
= false;
87 // This is just a copy for testing, since resources are added to the
88 // ResourceProvider in the pending tree.
89 delegated_layer
->resources_
= resources_
;
92 void DelegatedRendererLayerImpl::CreateChildIdIfNeeded(
93 const ReturnCallback
& return_callback
) {
97 ResourceProvider
* resource_provider
= layer_tree_impl()->resource_provider();
98 child_id_
= resource_provider
->CreateChild(return_callback
);
102 void DelegatedRendererLayerImpl::SetFrameData(
103 const DelegatedFrameData
* frame_data
,
104 const gfx::RectF
& damage_in_frame
) {
105 DCHECK(child_id_
) << "CreateChildIdIfNeeded must be called first.";
107 DCHECK(!frame_data
->render_pass_list
.empty());
108 // A frame with an empty root render pass is invalid.
109 DCHECK(!frame_data
->render_pass_list
.back()->output_rect
.IsEmpty());
111 ResourceProvider
* resource_provider
= layer_tree_impl()->resource_provider();
112 const ResourceProvider::ResourceIdMap
& resource_map
=
113 resource_provider
->GetChildToParentMap(child_id_
);
115 resource_provider
->ReceiveFromChild(child_id_
, frame_data
->resource_list
);
117 RenderPassList render_pass_list
;
118 RenderPass::CopyAll(frame_data
->render_pass_list
, &render_pass_list
);
120 bool invalid_frame
= false;
121 ResourceProvider::ResourceIdArray resources_in_frame
;
122 DrawQuad::ResourceIteratorCallback remap_resources_to_parent_callback
=
123 base::Bind(&ResourceRemapHelper
,
126 &resources_in_frame
);
127 for (size_t i
= 0; i
< render_pass_list
.size(); ++i
) {
128 RenderPass
* pass
= render_pass_list
[i
];
129 for (size_t j
= 0; j
< pass
->quad_list
.size(); ++j
) {
130 DrawQuad
* quad
= pass
->quad_list
[j
];
131 quad
->IterateResources(remap_resources_to_parent_callback
);
136 // Declare we are still using the last frame's resources.
137 resource_provider
->DeclareUsedResourcesFromChild(child_id_
, resources_
);
141 // Declare we are using the new frame's resources.
142 resources_
.swap(resources_in_frame
);
143 resource_provider
->DeclareUsedResourcesFromChild(child_id_
, resources_
);
145 inverse_device_scale_factor_
= 1.0f
/ frame_data
->device_scale_factor
;
146 // Display size is already set so we can compute what the damage rect
147 // will be in layer space. The damage may exceed the visible portion of
148 // the frame, so intersect the damage to the layer's bounds.
149 RenderPass
* new_root_pass
= render_pass_list
.back();
150 gfx::Size frame_size
= new_root_pass
->output_rect
.size();
151 gfx::RectF damage_in_layer
= damage_in_frame
;
152 damage_in_layer
.Scale(inverse_device_scale_factor_
);
153 SetUpdateRect(gfx::IntersectRects(
154 gfx::UnionRects(update_rect(), damage_in_layer
), gfx::Rect(bounds())));
156 SetRenderPasses(&render_pass_list
);
157 have_render_passes_to_push_
= true;
160 void DelegatedRendererLayerImpl::SetRenderPasses(
161 RenderPassList
* render_passes_in_draw_order
) {
164 for (size_t i
= 0; i
< render_passes_in_draw_order
->size(); ++i
) {
165 RenderPassList::iterator to_take
=
166 render_passes_in_draw_order
->begin() + i
;
167 render_passes_index_by_id_
.insert(
168 std::pair
<RenderPassId
, int>((*to_take
)->id
, i
));
169 scoped_ptr
<RenderPass
> taken_render_pass
=
170 render_passes_in_draw_order
->take(to_take
);
171 render_passes_in_draw_order_
.push_back(taken_render_pass
.Pass());
174 // Give back an empty array instead of nulls.
175 render_passes_in_draw_order
->clear();
178 void DelegatedRendererLayerImpl::ClearRenderPasses() {
179 render_passes_index_by_id_
.clear();
180 render_passes_in_draw_order_
.clear();
183 scoped_ptr
<LayerImpl
> DelegatedRendererLayerImpl::CreateLayerImpl(
184 LayerTreeImpl
* tree_impl
) {
185 return DelegatedRendererLayerImpl::Create(
186 tree_impl
, id()).PassAs
<LayerImpl
>();
189 void DelegatedRendererLayerImpl::ReleaseResources() {
194 static inline int IndexToId(int index
) { return index
+ 1; }
195 static inline int IdToIndex(int id
) { return id
- 1; }
197 RenderPassId
DelegatedRendererLayerImpl::FirstContributingRenderPassId() const {
198 return RenderPassId(id(), IndexToId(0));
201 RenderPassId
DelegatedRendererLayerImpl::NextContributingRenderPassId(
202 RenderPassId previous
) const {
203 return RenderPassId(previous
.layer_id
, previous
.index
+ 1);
206 bool DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
207 RenderPassId delegated_render_pass_id
,
208 RenderPassId
* output_render_pass_id
) const {
209 base::hash_map
<RenderPassId
, int>::const_iterator found
=
210 render_passes_index_by_id_
.find(delegated_render_pass_id
);
211 if (found
== render_passes_index_by_id_
.end()) {
212 // Be robust against a RenderPass id that isn't part of the frame.
215 unsigned delegated_render_pass_index
= found
->second
;
216 *output_render_pass_id
=
217 RenderPassId(id(), IndexToId(delegated_render_pass_index
));
221 void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
222 RenderPassSink
* render_pass_sink
) {
223 DCHECK(HasContributingDelegatedRenderPasses());
225 const RenderPass
* root_delegated_render_pass
=
226 render_passes_in_draw_order_
.back();
227 gfx::Size frame_size
= root_delegated_render_pass
->output_rect
.size();
228 gfx::Transform delegated_frame_to_root_transform
= screen_space_transform();
229 delegated_frame_to_root_transform
.Scale(inverse_device_scale_factor_
,
230 inverse_device_scale_factor_
);
232 for (size_t i
= 0; i
< render_passes_in_draw_order_
.size() - 1; ++i
) {
233 RenderPassId
output_render_pass_id(-1, -1);
235 ConvertDelegatedRenderPassId(render_passes_in_draw_order_
[i
]->id
,
236 &output_render_pass_id
);
238 // Don't clash with the RenderPass we generate if we own a RenderSurface.
239 DCHECK(present
) << render_passes_in_draw_order_
[i
]->id
.layer_id
<< ", "
240 << render_passes_in_draw_order_
[i
]->id
.index
;
241 DCHECK_GT(output_render_pass_id
.index
, 0);
243 scoped_ptr
<RenderPass
> copy_pass
=
244 render_passes_in_draw_order_
[i
]->Copy(output_render_pass_id
);
245 copy_pass
->transform_to_root_target
.ConcatTransform(
246 delegated_frame_to_root_transform
);
247 render_pass_sink
->AppendRenderPass(copy_pass
.Pass());
251 bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode
,
252 ResourceProvider
* resource_provider
) {
253 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
)
255 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
258 void DelegatedRendererLayerImpl::AppendQuads(
259 RenderPass
* render_pass
,
260 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
261 AppendQuadsData
* append_quads_data
) {
262 AppendRainbowDebugBorder(render_pass
, append_quads_data
);
264 // This list will be empty after a lost context until a new frame arrives.
265 if (render_passes_in_draw_order_
.empty())
268 RenderPassId target_render_pass_id
= append_quads_data
->render_pass_id
;
270 const RenderPass
* root_delegated_render_pass
=
271 render_passes_in_draw_order_
.back();
273 DCHECK(root_delegated_render_pass
->output_rect
.origin().IsOrigin());
274 gfx::Size frame_size
= root_delegated_render_pass
->output_rect
.size();
276 // If the index of the RenderPassId is 0, then it is a RenderPass generated
277 // for a layer in this compositor, not the delegating renderer. Then we want
278 // to merge our root RenderPass with the target RenderPass. Otherwise, it is
279 // some RenderPass which we added from the delegating renderer.
280 bool should_merge_root_render_pass_with_target
= !target_render_pass_id
.index
;
281 if (should_merge_root_render_pass_with_target
) {
282 // Verify that the RenderPass we are appending to is created by our
284 DCHECK(target_render_pass_id
.layer_id
== render_target()->id());
286 AppendRenderPassQuads(render_pass
,
289 root_delegated_render_pass
,
292 // Verify that the RenderPass we are appending to was created by us.
293 DCHECK(target_render_pass_id
.layer_id
== id());
295 int render_pass_index
= IdToIndex(target_render_pass_id
.index
);
296 const RenderPass
* delegated_render_pass
=
297 render_passes_in_draw_order_
[render_pass_index
];
298 AppendRenderPassQuads(render_pass
,
301 delegated_render_pass
,
306 void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
307 RenderPass
* render_pass
,
308 AppendQuadsData
* append_quads_data
) {
309 if (!ShowDebugBorders())
312 SharedQuadState
* shared_quad_state
=
313 render_pass
->CreateAndAppendSharedQuadState();
314 PopulateSharedQuadState(shared_quad_state
);
318 GetDebugBorderProperties(&color
, &border_width
);
322 0x80ffa500, // Orange.
323 0x80ffff00, // Yellow.
324 0x80008000, // Green.
326 0x80ee82ee, // Violet.
328 const int kNumColors
= arraysize(colors
);
330 const int kStripeWidth
= 300;
331 const int kStripeHeight
= 300;
333 for (size_t i
= 0; ; ++i
) {
334 // For horizontal lines.
335 int x
= kStripeWidth
* i
;
336 int width
= std::min(kStripeWidth
, content_bounds().width() - x
- 1);
338 // For vertical lines.
339 int y
= kStripeHeight
* i
;
340 int height
= std::min(kStripeHeight
, content_bounds().height() - y
- 1);
342 gfx::Rect
top(x
, 0, width
, border_width
);
344 content_bounds().height() - border_width
,
347 gfx::Rect
left(0, y
, border_width
, height
);
348 gfx::Rect
right(content_bounds().width() - border_width
,
353 if (top
.IsEmpty() && left
.IsEmpty())
356 if (!top
.IsEmpty()) {
357 SolidColorDrawQuad
* top_quad
=
358 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
360 shared_quad_state
, top
, top
, colors
[i
% kNumColors
], false);
362 SolidColorDrawQuad
* bottom_quad
=
363 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
364 bottom_quad
->SetNew(shared_quad_state
,
367 colors
[kNumColors
- 1 - (i
% kNumColors
)],
370 if (!left
.IsEmpty()) {
371 SolidColorDrawQuad
* left_quad
=
372 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
373 left_quad
->SetNew(shared_quad_state
,
376 colors
[kNumColors
- 1 - (i
% kNumColors
)],
379 SolidColorDrawQuad
* right_quad
=
380 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
382 shared_quad_state
, right
, right
, colors
[i
% kNumColors
], false);
387 void DelegatedRendererLayerImpl::AppendRenderPassQuads(
388 RenderPass
* render_pass
,
389 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
390 AppendQuadsData
* append_quads_data
,
391 const RenderPass
* delegated_render_pass
,
392 const gfx::Size
& frame_size
) const {
393 const SharedQuadState
* delegated_shared_quad_state
= NULL
;
394 SharedQuadState
* output_shared_quad_state
= NULL
;
396 for (size_t i
= 0; i
< delegated_render_pass
->quad_list
.size(); ++i
) {
397 const DrawQuad
* delegated_quad
= delegated_render_pass
->quad_list
[i
];
399 bool is_root_delegated_render_pass
=
400 delegated_render_pass
== render_passes_in_draw_order_
.back();
402 if (delegated_quad
->shared_quad_state
!= delegated_shared_quad_state
) {
403 delegated_shared_quad_state
= delegated_quad
->shared_quad_state
;
404 output_shared_quad_state
= render_pass
->CreateAndAppendSharedQuadState();
405 output_shared_quad_state
->CopyFrom(delegated_shared_quad_state
);
407 if (is_root_delegated_render_pass
) {
408 gfx::Transform delegated_frame_to_target_transform
= draw_transform();
409 delegated_frame_to_target_transform
.Scale(inverse_device_scale_factor_
,
410 inverse_device_scale_factor_
);
412 output_shared_quad_state
->content_to_target_transform
.ConcatTransform(
413 delegated_frame_to_target_transform
);
415 if (render_target() == this) {
416 DCHECK(!is_clipped());
417 DCHECK(render_surface());
418 DCHECK_EQ(0, num_unclipped_descendants());
419 output_shared_quad_state
->clip_rect
=
420 MathUtil::MapEnclosingClippedRect(
421 delegated_frame_to_target_transform
,
422 output_shared_quad_state
->clip_rect
);
424 gfx::Rect clip_rect
= drawable_content_rect();
425 if (output_shared_quad_state
->is_clipped
) {
426 clip_rect
.Intersect(MathUtil::MapEnclosingClippedRect(
427 delegated_frame_to_target_transform
,
428 output_shared_quad_state
->clip_rect
));
430 output_shared_quad_state
->clip_rect
= clip_rect
;
431 output_shared_quad_state
->is_clipped
= true;
434 output_shared_quad_state
->opacity
*= draw_opacity();
437 DCHECK(output_shared_quad_state
);
439 gfx::Transform quad_content_to_delegated_target_space
=
440 output_shared_quad_state
->content_to_target_transform
;
441 if (!is_root_delegated_render_pass
) {
442 quad_content_to_delegated_target_space
.ConcatTransform(
443 render_pass
->transform_to_root_target
);
444 quad_content_to_delegated_target_space
.ConcatTransform(draw_transform());
447 gfx::Rect quad_visible_rect
= occlusion_tracker
.UnoccludedContentRect(
448 delegated_quad
->visible_rect
, quad_content_to_delegated_target_space
);
450 if (quad_visible_rect
.IsEmpty())
453 if (delegated_quad
->material
!= DrawQuad::RENDER_PASS
) {
454 DrawQuad
* output_quad
= render_pass
->CopyFromAndAppendDrawQuad(
455 delegated_quad
, output_shared_quad_state
);
456 output_quad
->visible_rect
= quad_visible_rect
;
458 RenderPassId delegated_contributing_render_pass_id
=
459 RenderPassDrawQuad::MaterialCast(delegated_quad
)->render_pass_id
;
460 RenderPassId
output_contributing_render_pass_id(-1, -1);
463 ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id
,
464 &output_contributing_render_pass_id
);
466 // The frame may have a RenderPassDrawQuad that points to a RenderPass not
467 // part of the frame. Just ignore these quads.
469 DCHECK(output_contributing_render_pass_id
!=
470 append_quads_data
->render_pass_id
);
472 RenderPassDrawQuad
* output_quad
=
473 render_pass
->CopyFromAndAppendRenderPassDrawQuad(
474 RenderPassDrawQuad::MaterialCast(delegated_quad
),
475 output_shared_quad_state
,
476 output_contributing_render_pass_id
);
477 output_quad
->visible_rect
= quad_visible_rect
;
483 const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
484 return "cc::DelegatedRendererLayerImpl";
487 void DelegatedRendererLayerImpl::ClearChildId() {
492 ResourceProvider
* provider
= layer_tree_impl()->resource_provider();
493 provider
->DestroyChild(child_id_
);