Adding a logging class to cast.
[chromium-blink-merge.git] / cc / layers / delegated_renderer_layer_impl.cc
blob35bd0b7f1b3c5cea768fb98e345e52dc556941ca
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"
7 #include <algorithm>
8 #include <utility>
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/quad_sink.h"
15 #include "cc/layers/render_pass_sink.h"
16 #include "cc/output/delegated_frame_data.h"
17 #include "cc/quads/render_pass_draw_quad.h"
18 #include "cc/quads/solid_color_draw_quad.h"
19 #include "cc/trees/layer_tree_impl.h"
21 namespace cc {
23 DelegatedRendererLayerImpl::DelegatedRendererLayerImpl(
24 LayerTreeImpl* tree_impl, int id)
25 : LayerImpl(tree_impl, id),
26 have_render_passes_to_push_(false),
27 child_id_(0),
28 own_child_id_(false) {
31 DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
32 ClearRenderPasses();
33 ClearChildId();
36 bool DelegatedRendererLayerImpl::HasDelegatedContent() const { return true; }
38 bool DelegatedRendererLayerImpl::HasContributingDelegatedRenderPasses() const {
39 // The root RenderPass for the layer is merged with its target
40 // RenderPass in each frame. So we only have extra RenderPasses
41 // to merge when we have a non-root RenderPass present.
42 return render_passes_in_draw_order_.size() > 1;
45 static ResourceProvider::ResourceId ResourceRemapHelper(
46 bool* invalid_frame,
47 const ResourceProvider::ResourceIdMap& child_to_parent_map,
48 ResourceProvider::ResourceIdArray* resources_in_frame,
49 ResourceProvider::ResourceId id) {
51 ResourceProvider::ResourceIdMap::const_iterator it =
52 child_to_parent_map.find(id);
53 if (it == child_to_parent_map.end()) {
54 *invalid_frame = true;
55 return 0;
58 DCHECK_EQ(it->first, id);
59 ResourceProvider::ResourceId remapped_id = it->second;
60 resources_in_frame->push_back(id);
61 return remapped_id;
64 void DelegatedRendererLayerImpl::PushPropertiesTo(LayerImpl* layer) {
65 LayerImpl::PushPropertiesTo(layer);
67 DelegatedRendererLayerImpl* delegated_layer =
68 static_cast<DelegatedRendererLayerImpl*>(layer);
70 // If we have a new child_id to give to the active layer, it should
71 // have already deleted its old child_id.
72 DCHECK(delegated_layer->child_id_ == 0 ||
73 delegated_layer->child_id_ == child_id_);
74 delegated_layer->child_id_ = child_id_;
75 delegated_layer->own_child_id_ = true;
76 own_child_id_ = false;
78 delegated_layer->SetDisplaySize(display_size_);
79 if (have_render_passes_to_push_) {
80 // This passes ownership of the render passes to the active tree.
81 delegated_layer->SetRenderPasses(&render_passes_in_draw_order_);
82 DCHECK(render_passes_in_draw_order_.empty());
83 have_render_passes_to_push_ = false;
86 // This is just a copy for testing, since resources are added to the
87 // ResourceProvider in the pending tree.
88 delegated_layer->resources_ = resources_;
91 void DelegatedRendererLayerImpl::CreateChildIdIfNeeded(
92 const ReturnCallback& return_callback) {
93 if (child_id_)
94 return;
96 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
97 child_id_ = resource_provider->CreateChild(return_callback);
98 own_child_id_ = true;
101 void DelegatedRendererLayerImpl::SetFrameData(
102 const DelegatedFrameData* frame_data,
103 gfx::RectF damage_in_frame) {
104 DCHECK(child_id_) << "CreateChildIdIfNeeded must be called first.";
105 DCHECK(frame_data);
106 DCHECK(!frame_data->render_pass_list.empty());
107 // A frame with an empty root render pass is invalid.
108 DCHECK(!frame_data->render_pass_list.back()->output_rect.IsEmpty());
110 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
111 const ResourceProvider::ResourceIdMap& resource_map =
112 resource_provider->GetChildToParentMap(child_id_);
114 resource_provider->ReceiveFromChild(child_id_, frame_data->resource_list);
116 ScopedPtrVector<RenderPass> render_pass_list;
117 RenderPass::CopyAll(frame_data->render_pass_list, &render_pass_list);
119 bool invalid_frame = false;
120 ResourceProvider::ResourceIdArray resources_in_frame;
121 DrawQuad::ResourceIteratorCallback remap_resources_to_parent_callback =
122 base::Bind(&ResourceRemapHelper,
123 &invalid_frame,
124 resource_map,
125 &resources_in_frame);
126 for (size_t i = 0; i < render_pass_list.size(); ++i) {
127 RenderPass* pass = render_pass_list[i];
128 for (size_t j = 0; j < pass->quad_list.size(); ++j) {
129 DrawQuad* quad = pass->quad_list[j];
130 quad->IterateResources(remap_resources_to_parent_callback);
134 if (invalid_frame) {
135 // Declare we are still using the last frame's resources.
136 resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
137 return;
140 // Declare we are using the new frame's resources.
141 resources_.swap(resources_in_frame);
142 resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
144 // Display size is already set so we can compute what the damage rect
145 // will be in layer space.
146 RenderPass* new_root_pass = render_pass_list.back();
147 gfx::Size frame_size = new_root_pass->output_rect.size();
148 gfx::RectF damage_in_layer = MathUtil::MapClippedRect(
149 DelegatedFrameToLayerSpaceTransform(frame_size), damage_in_frame);
150 set_update_rect(gfx::UnionRects(update_rect(), damage_in_layer));
152 SetRenderPasses(&render_pass_list);
153 have_render_passes_to_push_ = true;
156 void DelegatedRendererLayerImpl::SetDisplaySize(gfx::Size size) {
157 if (display_size_ == size)
158 return;
159 display_size_ = size;
160 NoteLayerPropertyChanged();
163 void DelegatedRendererLayerImpl::SetRenderPasses(
164 ScopedPtrVector<RenderPass>* render_passes_in_draw_order) {
165 ClearRenderPasses();
167 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
168 ScopedPtrVector<RenderPass>::iterator to_take =
169 render_passes_in_draw_order->begin() + i;
170 render_passes_index_by_id_.insert(
171 std::pair<RenderPass::Id, int>((*to_take)->id, i));
172 scoped_ptr<RenderPass> taken_render_pass =
173 render_passes_in_draw_order->take(to_take);
174 render_passes_in_draw_order_.push_back(taken_render_pass.Pass());
177 // Give back an empty array instead of nulls.
178 render_passes_in_draw_order->clear();
181 void DelegatedRendererLayerImpl::ClearRenderPasses() {
182 render_passes_index_by_id_.clear();
183 render_passes_in_draw_order_.clear();
186 scoped_ptr<LayerImpl> DelegatedRendererLayerImpl::CreateLayerImpl(
187 LayerTreeImpl* tree_impl) {
188 return DelegatedRendererLayerImpl::Create(
189 tree_impl, id()).PassAs<LayerImpl>();
192 void DelegatedRendererLayerImpl::DidLoseOutputSurface() {
193 ClearRenderPasses();
194 ClearChildId();
197 gfx::Transform DelegatedRendererLayerImpl::DelegatedFrameToLayerSpaceTransform(
198 gfx::Size frame_size) const {
199 gfx::Size display_size = display_size_.IsEmpty() ? bounds() : display_size_;
201 gfx::Transform delegated_frame_to_layer_space_transform;
202 delegated_frame_to_layer_space_transform.Scale(
203 static_cast<double>(display_size.width()) / frame_size.width(),
204 static_cast<double>(display_size.height()) / frame_size.height());
205 return delegated_frame_to_layer_space_transform;
208 static inline int IndexToId(int index) { return index + 1; }
209 static inline int IdToIndex(int id) { return id - 1; }
211 RenderPass::Id DelegatedRendererLayerImpl::FirstContributingRenderPassId()
212 const {
213 return RenderPass::Id(id(), IndexToId(0));
216 RenderPass::Id DelegatedRendererLayerImpl::NextContributingRenderPassId(
217 RenderPass::Id previous) const {
218 return RenderPass::Id(previous.layer_id, previous.index + 1);
221 bool DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
222 RenderPass::Id delegated_render_pass_id,
223 RenderPass::Id* output_render_pass_id) const {
224 base::hash_map<RenderPass::Id, int>::const_iterator found =
225 render_passes_index_by_id_.find(delegated_render_pass_id);
226 if (found == render_passes_index_by_id_.end()) {
227 // Be robust against a RenderPass id that isn't part of the frame.
228 return false;
230 unsigned delegated_render_pass_index = found->second;
231 *output_render_pass_id =
232 RenderPass::Id(id(), IndexToId(delegated_render_pass_index));
233 return true;
236 void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
237 RenderPassSink* render_pass_sink) {
238 DCHECK(HasContributingDelegatedRenderPasses());
240 for (size_t i = 0; i < render_passes_in_draw_order_.size() - 1; ++i) {
241 RenderPass::Id output_render_pass_id(-1, -1);
242 bool present =
243 ConvertDelegatedRenderPassId(render_passes_in_draw_order_[i]->id,
244 &output_render_pass_id);
246 // Don't clash with the RenderPass we generate if we own a RenderSurface.
247 DCHECK(present) << render_passes_in_draw_order_[i]->id.layer_id << ", "
248 << render_passes_in_draw_order_[i]->id.index;
249 DCHECK_GT(output_render_pass_id.index, 0);
251 render_pass_sink->AppendRenderPass(
252 render_passes_in_draw_order_[i]->Copy(output_render_pass_id));
256 bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode,
257 ResourceProvider* resource_provider) {
258 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
259 return false;
260 return LayerImpl::WillDraw(draw_mode, resource_provider);
263 void DelegatedRendererLayerImpl::AppendQuads(
264 QuadSink* quad_sink,
265 AppendQuadsData* append_quads_data) {
266 AppendRainbowDebugBorder(quad_sink, append_quads_data);
268 // This list will be empty after a lost context until a new frame arrives.
269 if (render_passes_in_draw_order_.empty())
270 return;
272 RenderPass::Id target_render_pass_id = append_quads_data->render_pass_id;
274 const RenderPass* root_delegated_render_pass =
275 render_passes_in_draw_order_.back();
277 DCHECK(root_delegated_render_pass->output_rect.origin().IsOrigin());
278 gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
280 // If the index of the RenderPassId is 0, then it is a RenderPass generated
281 // for a layer in this compositor, not the delegating renderer. Then we want
282 // to merge our root RenderPass with the target RenderPass. Otherwise, it is
283 // some RenderPass which we added from the delegating renderer.
284 bool should_merge_root_render_pass_with_target = !target_render_pass_id.index;
285 if (should_merge_root_render_pass_with_target) {
286 // Verify that the RenderPass we are appending to is created by our
287 // render_target.
288 DCHECK(target_render_pass_id.layer_id == render_target()->id());
290 AppendRenderPassQuads(
291 quad_sink, append_quads_data, root_delegated_render_pass, frame_size);
292 } else {
293 // Verify that the RenderPass we are appending to was created by us.
294 DCHECK(target_render_pass_id.layer_id == id());
296 int render_pass_index = IdToIndex(target_render_pass_id.index);
297 const RenderPass* delegated_render_pass =
298 render_passes_in_draw_order_[render_pass_index];
299 AppendRenderPassQuads(
300 quad_sink, append_quads_data, delegated_render_pass, frame_size);
304 void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
305 QuadSink* quad_sink,
306 AppendQuadsData* append_quads_data) {
307 if (!ShowDebugBorders())
308 return;
310 SharedQuadState* shared_quad_state =
311 quad_sink->UseSharedQuadState(CreateSharedQuadState());
313 SkColor color;
314 float border_width;
315 GetDebugBorderProperties(&color, &border_width);
317 SkColor colors[] = {
318 0x80ff0000, // Red.
319 0x80ffa500, // Orange.
320 0x80ffff00, // Yellow.
321 0x80008000, // Green.
322 0x800000ff, // Blue.
323 0x80ee82ee, // Violet.
325 const int kNumColors = arraysize(colors);
327 const int kStripeWidth = 300;
328 const int kStripeHeight = 300;
330 for (size_t i = 0; ; ++i) {
331 // For horizontal lines.
332 int x = kStripeWidth * i;
333 int width = std::min(kStripeWidth, content_bounds().width() - x - 1);
335 // For vertical lines.
336 int y = kStripeHeight * i;
337 int height = std::min(kStripeHeight, content_bounds().height() - y - 1);
339 gfx::Rect top(x, 0, width, border_width);
340 gfx::Rect bottom(x,
341 content_bounds().height() - border_width,
342 width,
343 border_width);
344 gfx::Rect left(0, y, border_width, height);
345 gfx::Rect right(content_bounds().width() - border_width,
347 border_width,
348 height);
350 if (top.IsEmpty() && left.IsEmpty())
351 break;
353 if (!top.IsEmpty()) {
354 scoped_ptr<SolidColorDrawQuad> top_quad = SolidColorDrawQuad::Create();
355 top_quad->SetNew(shared_quad_state, top, colors[i % kNumColors], false);
356 quad_sink->Append(top_quad.PassAs<DrawQuad>(), append_quads_data);
358 scoped_ptr<SolidColorDrawQuad> bottom_quad = SolidColorDrawQuad::Create();
359 bottom_quad->SetNew(shared_quad_state,
360 bottom,
361 colors[kNumColors - 1 - (i % kNumColors)],
362 false);
363 quad_sink->Append(bottom_quad.PassAs<DrawQuad>(), append_quads_data);
365 if (!left.IsEmpty()) {
366 scoped_ptr<SolidColorDrawQuad> left_quad = SolidColorDrawQuad::Create();
367 left_quad->SetNew(shared_quad_state,
368 left,
369 colors[kNumColors - 1 - (i % kNumColors)],
370 false);
371 quad_sink->Append(left_quad.PassAs<DrawQuad>(), append_quads_data);
373 scoped_ptr<SolidColorDrawQuad> right_quad = SolidColorDrawQuad::Create();
374 right_quad->SetNew(
375 shared_quad_state, right, colors[i % kNumColors], false);
376 quad_sink->Append(right_quad.PassAs<DrawQuad>(), append_quads_data);
381 void DelegatedRendererLayerImpl::AppendRenderPassQuads(
382 QuadSink* quad_sink,
383 AppendQuadsData* append_quads_data,
384 const RenderPass* delegated_render_pass,
385 gfx::Size frame_size) const {
387 const SharedQuadState* delegated_shared_quad_state = NULL;
388 SharedQuadState* output_shared_quad_state = NULL;
390 for (size_t i = 0; i < delegated_render_pass->quad_list.size(); ++i) {
391 const DrawQuad* delegated_quad = delegated_render_pass->quad_list[i];
393 if (delegated_quad->shared_quad_state != delegated_shared_quad_state) {
394 delegated_shared_quad_state = delegated_quad->shared_quad_state;
395 output_shared_quad_state = quad_sink->UseSharedQuadState(
396 delegated_shared_quad_state->Copy());
398 bool is_root_delegated_render_pass =
399 delegated_render_pass == render_passes_in_draw_order_.back();
400 if (is_root_delegated_render_pass) {
401 // Don't allow areas inside the bounds that are empty.
402 DCHECK(display_size_.IsEmpty() ||
403 gfx::Rect(display_size_).Contains(gfx::Rect(bounds())));
404 gfx::Transform delegated_frame_to_target_transform =
405 draw_transform() * DelegatedFrameToLayerSpaceTransform(frame_size);
407 output_shared_quad_state->content_to_target_transform.ConcatTransform(
408 delegated_frame_to_target_transform);
410 if (render_target() == this) {
411 DCHECK(!is_clipped());
412 DCHECK(render_surface());
413 DCHECK_EQ(0, num_unclipped_descendants());
414 output_shared_quad_state->clip_rect = MathUtil::MapClippedRect(
415 delegated_frame_to_target_transform,
416 output_shared_quad_state->clip_rect);
417 } else {
418 gfx::Rect clip_rect = drawable_content_rect();
419 if (output_shared_quad_state->is_clipped) {
420 clip_rect.Intersect(MathUtil::MapClippedRect(
421 delegated_frame_to_target_transform,
422 output_shared_quad_state->clip_rect));
424 output_shared_quad_state->clip_rect = clip_rect;
425 output_shared_quad_state->is_clipped = true;
428 output_shared_quad_state->opacity *= draw_opacity();
431 DCHECK(output_shared_quad_state);
433 scoped_ptr<DrawQuad> output_quad;
434 if (delegated_quad->material != DrawQuad::RENDER_PASS) {
435 output_quad = delegated_quad->Copy(output_shared_quad_state);
436 } else {
437 RenderPass::Id delegated_contributing_render_pass_id =
438 RenderPassDrawQuad::MaterialCast(delegated_quad)->render_pass_id;
439 RenderPass::Id output_contributing_render_pass_id(-1, -1);
441 bool present =
442 ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id,
443 &output_contributing_render_pass_id);
445 // The frame may have a RenderPassDrawQuad that points to a RenderPass not
446 // part of the frame. Just ignore these quads.
447 if (present) {
448 DCHECK(output_contributing_render_pass_id !=
449 append_quads_data->render_pass_id);
451 output_quad = RenderPassDrawQuad::MaterialCast(delegated_quad)->Copy(
452 output_shared_quad_state,
453 output_contributing_render_pass_id).PassAs<DrawQuad>();
457 if (output_quad)
458 quad_sink->Append(output_quad.Pass(), append_quads_data);
462 const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
463 return "cc::DelegatedRendererLayerImpl";
466 void DelegatedRendererLayerImpl::ClearChildId() {
467 if (!child_id_)
468 return;
470 if (own_child_id_) {
471 ResourceProvider* provider = layer_tree_impl()->resource_provider();
472 provider->DestroyChild(child_id_);
475 child_id_ = 0;
478 } // namespace cc