blob: 8a95b9e9137f26dbb68ad2241011a5132fc9cde2 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
5#include "cc/layers/delegated_renderer_layer_impl.h"
6
7#include <algorithm>
8#include <utility>
9
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"
James Robinsonbaf71d32014-10-08 13:00:20 -070019#include "cc/trees/occlusion.h"
James Robinsone2ac7e82014-10-15 13:21:59 -070020#include "ui/gfx/geometry/rect_conversions.h"
James Robinson646469d2014-10-03 15:33:28 -070021
22namespace cc {
23
24DelegatedRendererLayerImpl::DelegatedRendererLayerImpl(LayerTreeImpl* tree_impl,
25 int id)
26 : LayerImpl(tree_impl, id),
27 have_render_passes_to_push_(false),
28 inverse_device_scale_factor_(1.0f),
29 child_id_(0),
30 own_child_id_(false) {
31}
32
33DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
34 ClearRenderPasses();
35 ClearChildId();
36}
37
38bool DelegatedRendererLayerImpl::HasDelegatedContent() const { return true; }
39
40bool DelegatedRendererLayerImpl::HasContributingDelegatedRenderPasses() const {
41 // The root RenderPass for the layer is merged with its target
42 // RenderPass in each frame. So we only have extra RenderPasses
43 // to merge when we have a non-root RenderPass present.
44 return render_passes_in_draw_order_.size() > 1;
45}
46
47static ResourceProvider::ResourceId ResourceRemapHelper(
48 bool* invalid_frame,
49 const ResourceProvider::ResourceIdMap& child_to_parent_map,
50 ResourceProvider::ResourceIdArray* resources_in_frame,
51 ResourceProvider::ResourceId id) {
52
53 ResourceProvider::ResourceIdMap::const_iterator it =
54 child_to_parent_map.find(id);
55 if (it == child_to_parent_map.end()) {
56 *invalid_frame = true;
57 return 0;
58 }
59
60 DCHECK_EQ(it->first, id);
61 ResourceProvider::ResourceId remapped_id = it->second;
62 resources_in_frame->push_back(id);
63 return remapped_id;
64}
65
66void DelegatedRendererLayerImpl::PushPropertiesTo(LayerImpl* layer) {
67 LayerImpl::PushPropertiesTo(layer);
68
69 DelegatedRendererLayerImpl* delegated_layer =
70 static_cast<DelegatedRendererLayerImpl*>(layer);
71
72 // If we have a new child_id to give to the active layer, it should
73 // have already deleted its old child_id.
74 DCHECK(delegated_layer->child_id_ == 0 ||
75 delegated_layer->child_id_ == child_id_);
76 delegated_layer->inverse_device_scale_factor_ = inverse_device_scale_factor_;
77 delegated_layer->child_id_ = child_id_;
78 delegated_layer->own_child_id_ = true;
79 own_child_id_ = false;
80
81 if (have_render_passes_to_push_) {
82 // This passes ownership of the render passes to the active tree.
83 delegated_layer->SetRenderPasses(&render_passes_in_draw_order_);
84 DCHECK(render_passes_in_draw_order_.empty());
85 have_render_passes_to_push_ = false;
86 }
87
88 // This is just a copy for testing, since resources are added to the
89 // ResourceProvider in the pending tree.
90 delegated_layer->resources_ = resources_;
91}
92
93void DelegatedRendererLayerImpl::CreateChildIdIfNeeded(
94 const ReturnCallback& return_callback) {
95 if (child_id_)
96 return;
97
98 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
99 child_id_ = resource_provider->CreateChild(return_callback);
100 own_child_id_ = true;
101}
102
103void DelegatedRendererLayerImpl::SetFrameData(
104 const DelegatedFrameData* frame_data,
105 const gfx::RectF& damage_in_frame) {
106 DCHECK(child_id_) << "CreateChildIdIfNeeded must be called first.";
107 DCHECK(frame_data);
108 DCHECK(!frame_data->render_pass_list.empty());
109 // A frame with an empty root render pass is invalid.
110 DCHECK(!frame_data->render_pass_list.back()->output_rect.IsEmpty());
111
112 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
113 const ResourceProvider::ResourceIdMap& resource_map =
114 resource_provider->GetChildToParentMap(child_id_);
115
116 resource_provider->ReceiveFromChild(child_id_, frame_data->resource_list);
117
118 RenderPassList render_pass_list;
119 RenderPass::CopyAll(frame_data->render_pass_list, &render_pass_list);
120
121 bool invalid_frame = false;
122 ResourceProvider::ResourceIdArray resources_in_frame;
123 DrawQuad::ResourceIteratorCallback remap_resources_to_parent_callback =
124 base::Bind(&ResourceRemapHelper,
125 &invalid_frame,
126 resource_map,
127 &resources_in_frame);
James Robinson7f480212014-10-31 10:28:08 -0700128 for (const auto& pass : render_pass_list) {
129 for (const auto& quad : pass->quad_list)
130 quad->IterateResources(remap_resources_to_parent_callback);
James Robinson646469d2014-10-03 15:33:28 -0700131 }
132
133 if (invalid_frame) {
134 // Declare we are still using the last frame's resources.
135 resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
136 return;
137 }
138
139 // Declare we are using the new frame's resources.
140 resources_.swap(resources_in_frame);
141 resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
142
143 inverse_device_scale_factor_ = 1.0f / frame_data->device_scale_factor;
144 // Display size is already set so we can compute what the damage rect
145 // will be in layer space. The damage may exceed the visible portion of
146 // the frame, so intersect the damage to the layer's bounds.
147 RenderPass* new_root_pass = render_pass_list.back();
148 gfx::Size frame_size = new_root_pass->output_rect.size();
149 gfx::RectF damage_in_layer = damage_in_frame;
150 damage_in_layer.Scale(inverse_device_scale_factor_);
151 SetUpdateRect(gfx::IntersectRects(
James Robinsone2ac7e82014-10-15 13:21:59 -0700152 gfx::UnionRects(update_rect(), gfx::ToEnclosingRect(damage_in_layer)),
153 gfx::Rect(bounds())));
James Robinson646469d2014-10-03 15:33:28 -0700154
155 SetRenderPasses(&render_pass_list);
156 have_render_passes_to_push_ = true;
157}
158
159void DelegatedRendererLayerImpl::SetRenderPasses(
160 RenderPassList* render_passes_in_draw_order) {
161 ClearRenderPasses();
162
163 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
164 RenderPassList::iterator to_take =
165 render_passes_in_draw_order->begin() + i;
166 render_passes_index_by_id_.insert(
167 std::pair<RenderPassId, int>((*to_take)->id, i));
168 scoped_ptr<RenderPass> taken_render_pass =
169 render_passes_in_draw_order->take(to_take);
170 render_passes_in_draw_order_.push_back(taken_render_pass.Pass());
171 }
172
173 // Give back an empty array instead of nulls.
174 render_passes_in_draw_order->clear();
175}
176
177void DelegatedRendererLayerImpl::ClearRenderPasses() {
178 render_passes_index_by_id_.clear();
179 render_passes_in_draw_order_.clear();
180}
181
182scoped_ptr<LayerImpl> DelegatedRendererLayerImpl::CreateLayerImpl(
183 LayerTreeImpl* tree_impl) {
184 return DelegatedRendererLayerImpl::Create(tree_impl, id());
185}
186
187void DelegatedRendererLayerImpl::ReleaseResources() {
188 ClearRenderPasses();
189 ClearChildId();
190}
191
192static inline int IndexToId(int index) { return index + 1; }
193static inline int IdToIndex(int id) { return id - 1; }
194
195RenderPassId DelegatedRendererLayerImpl::FirstContributingRenderPassId() const {
196 return RenderPassId(id(), IndexToId(0));
197}
198
199RenderPassId DelegatedRendererLayerImpl::NextContributingRenderPassId(
200 RenderPassId previous) const {
201 return RenderPassId(previous.layer_id, previous.index + 1);
202}
203
204bool DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
205 RenderPassId delegated_render_pass_id,
206 RenderPassId* output_render_pass_id) const {
207 base::hash_map<RenderPassId, int>::const_iterator found =
208 render_passes_index_by_id_.find(delegated_render_pass_id);
209 if (found == render_passes_index_by_id_.end()) {
210 // Be robust against a RenderPass id that isn't part of the frame.
211 return false;
212 }
213 unsigned delegated_render_pass_index = found->second;
214 *output_render_pass_id =
215 RenderPassId(id(), IndexToId(delegated_render_pass_index));
216 return true;
217}
218
219void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
220 RenderPassSink* render_pass_sink) {
221 DCHECK(HasContributingDelegatedRenderPasses());
222
223 const RenderPass* root_delegated_render_pass =
224 render_passes_in_draw_order_.back();
225 gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
226 gfx::Transform delegated_frame_to_root_transform = screen_space_transform();
227 delegated_frame_to_root_transform.Scale(inverse_device_scale_factor_,
228 inverse_device_scale_factor_);
229
230 for (size_t i = 0; i < render_passes_in_draw_order_.size() - 1; ++i) {
231 RenderPassId output_render_pass_id(-1, -1);
232 bool present =
233 ConvertDelegatedRenderPassId(render_passes_in_draw_order_[i]->id,
234 &output_render_pass_id);
235
236 // Don't clash with the RenderPass we generate if we own a RenderSurface.
237 DCHECK(present) << render_passes_in_draw_order_[i]->id.layer_id << ", "
238 << render_passes_in_draw_order_[i]->id.index;
239 DCHECK_GT(output_render_pass_id.index, 0);
240
241 scoped_ptr<RenderPass> copy_pass =
242 render_passes_in_draw_order_[i]->Copy(output_render_pass_id);
243 copy_pass->transform_to_root_target.ConcatTransform(
244 delegated_frame_to_root_transform);
245 render_pass_sink->AppendRenderPass(copy_pass.Pass());
246 }
247}
248
249bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode,
250 ResourceProvider* resource_provider) {
251 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
252 return false;
253 return LayerImpl::WillDraw(draw_mode, resource_provider);
254}
255
256void DelegatedRendererLayerImpl::AppendQuads(
257 RenderPass* render_pass,
James Robinsonbaf71d32014-10-08 13:00:20 -0700258 const Occlusion& occlusion_in_content_space,
James Robinson646469d2014-10-03 15:33:28 -0700259 AppendQuadsData* append_quads_data) {
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100260 AppendRainbowDebugBorder(render_pass);
James Robinson646469d2014-10-03 15:33:28 -0700261
262 // This list will be empty after a lost context until a new frame arrives.
263 if (render_passes_in_draw_order_.empty())
264 return;
265
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100266 RenderPassId target_render_pass_id = render_pass->id;
James Robinson646469d2014-10-03 15:33:28 -0700267
268 const RenderPass* root_delegated_render_pass =
269 render_passes_in_draw_order_.back();
270
271 DCHECK(root_delegated_render_pass->output_rect.origin().IsOrigin());
272 gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
273
274 // If the index of the RenderPassId is 0, then it is a RenderPass generated
275 // for a layer in this compositor, not the delegating renderer. Then we want
276 // to merge our root RenderPass with the target RenderPass. Otherwise, it is
277 // some RenderPass which we added from the delegating renderer.
278 bool should_merge_root_render_pass_with_target = !target_render_pass_id.index;
279 if (should_merge_root_render_pass_with_target) {
280 // Verify that the RenderPass we are appending to is created by our
281 // render_target.
282 DCHECK(target_render_pass_id.layer_id == render_target()->id());
283
284 AppendRenderPassQuads(render_pass,
James Robinsonbaf71d32014-10-08 13:00:20 -0700285 occlusion_in_content_space,
James Robinson646469d2014-10-03 15:33:28 -0700286 root_delegated_render_pass,
287 frame_size);
288 } else {
289 // Verify that the RenderPass we are appending to was created by us.
290 DCHECK(target_render_pass_id.layer_id == id());
291
292 int render_pass_index = IdToIndex(target_render_pass_id.index);
293 const RenderPass* delegated_render_pass =
294 render_passes_in_draw_order_[render_pass_index];
295 AppendRenderPassQuads(render_pass,
James Robinsonbaf71d32014-10-08 13:00:20 -0700296 occlusion_in_content_space,
James Robinson646469d2014-10-03 15:33:28 -0700297 delegated_render_pass,
298 frame_size);
299 }
300}
301
302void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100303 RenderPass* render_pass) {
James Robinson646469d2014-10-03 15:33:28 -0700304 if (!ShowDebugBorders())
305 return;
306
307 SharedQuadState* shared_quad_state =
308 render_pass->CreateAndAppendSharedQuadState();
309 PopulateSharedQuadState(shared_quad_state);
310
311 SkColor color;
312 float border_width;
313 GetDebugBorderProperties(&color, &border_width);
314
315 SkColor colors[] = {
316 0x80ff0000, // Red.
317 0x80ffa500, // Orange.
318 0x80ffff00, // Yellow.
319 0x80008000, // Green.
320 0x800000ff, // Blue.
321 0x80ee82ee, // Violet.
322 };
323 const int kNumColors = arraysize(colors);
324
325 const int kStripeWidth = 300;
326 const int kStripeHeight = 300;
327
328 for (size_t i = 0; ; ++i) {
329 // For horizontal lines.
330 int x = kStripeWidth * i;
331 int width = std::min(kStripeWidth, content_bounds().width() - x - 1);
332
333 // For vertical lines.
334 int y = kStripeHeight * i;
335 int height = std::min(kStripeHeight, content_bounds().height() - y - 1);
336
337 gfx::Rect top(x, 0, width, border_width);
338 gfx::Rect bottom(x,
339 content_bounds().height() - border_width,
340 width,
341 border_width);
342 gfx::Rect left(0, y, border_width, height);
343 gfx::Rect right(content_bounds().width() - border_width,
344 y,
345 border_width,
346 height);
347
348 if (top.IsEmpty() && left.IsEmpty())
349 break;
350
351 if (!top.IsEmpty()) {
352 SolidColorDrawQuad* top_quad =
353 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
354 top_quad->SetNew(
355 shared_quad_state, top, top, colors[i % kNumColors], false);
356
357 SolidColorDrawQuad* bottom_quad =
358 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
359 bottom_quad->SetNew(shared_quad_state,
360 bottom,
361 bottom,
362 colors[kNumColors - 1 - (i % kNumColors)],
363 false);
364 }
365 if (!left.IsEmpty()) {
366 SolidColorDrawQuad* left_quad =
367 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
368 left_quad->SetNew(shared_quad_state,
369 left,
370 left,
371 colors[kNumColors - 1 - (i % kNumColors)],
372 false);
373
374 SolidColorDrawQuad* right_quad =
375 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
376 right_quad->SetNew(
377 shared_quad_state, right, right, colors[i % kNumColors], false);
378 }
379 }
380}
381
382void DelegatedRendererLayerImpl::AppendRenderPassQuads(
383 RenderPass* render_pass,
James Robinsonbaf71d32014-10-08 13:00:20 -0700384 const Occlusion& occlusion_in_content_space,
James Robinson646469d2014-10-03 15:33:28 -0700385 const RenderPass* delegated_render_pass,
386 const gfx::Size& frame_size) const {
James Robinsone2ac7e82014-10-15 13:21:59 -0700387 const SharedQuadState* delegated_shared_quad_state = nullptr;
388 SharedQuadState* output_shared_quad_state = nullptr;
James Robinson646469d2014-10-03 15:33:28 -0700389
James Robinsonbaf71d32014-10-08 13:00:20 -0700390 for (const auto& delegated_quad : delegated_render_pass->quad_list) {
James Robinson646469d2014-10-03 15:33:28 -0700391 bool is_root_delegated_render_pass =
392 delegated_render_pass == render_passes_in_draw_order_.back();
393
James Robinson7f480212014-10-31 10:28:08 -0700394 if (delegated_quad->shared_quad_state != delegated_shared_quad_state) {
395 delegated_shared_quad_state = delegated_quad->shared_quad_state;
James Robinson646469d2014-10-03 15:33:28 -0700396 output_shared_quad_state = render_pass->CreateAndAppendSharedQuadState();
397 output_shared_quad_state->CopyFrom(delegated_shared_quad_state);
398
399 if (is_root_delegated_render_pass) {
400 gfx::Transform delegated_frame_to_target_transform = draw_transform();
401 delegated_frame_to_target_transform.Scale(inverse_device_scale_factor_,
402 inverse_device_scale_factor_);
403
404 output_shared_quad_state->content_to_target_transform.ConcatTransform(
405 delegated_frame_to_target_transform);
406
407 if (render_target() == this) {
408 DCHECK(!is_clipped());
409 DCHECK(render_surface());
410 DCHECK_EQ(0, num_unclipped_descendants());
411 output_shared_quad_state->clip_rect =
412 MathUtil::MapEnclosingClippedRect(
413 delegated_frame_to_target_transform,
414 output_shared_quad_state->clip_rect);
415 } else {
416 gfx::Rect clip_rect = drawable_content_rect();
417 if (output_shared_quad_state->is_clipped) {
418 clip_rect.Intersect(MathUtil::MapEnclosingClippedRect(
419 delegated_frame_to_target_transform,
420 output_shared_quad_state->clip_rect));
421 }
422 output_shared_quad_state->clip_rect = clip_rect;
423 output_shared_quad_state->is_clipped = true;
424 }
425
426 output_shared_quad_state->opacity *= draw_opacity();
427 }
428 }
429 DCHECK(output_shared_quad_state);
430
431 gfx::Transform quad_content_to_delegated_target_space =
432 output_shared_quad_state->content_to_target_transform;
433 if (!is_root_delegated_render_pass) {
434 quad_content_to_delegated_target_space.ConcatTransform(
435 delegated_render_pass->transform_to_root_target);
436 quad_content_to_delegated_target_space.ConcatTransform(draw_transform());
437 }
438
James Robinsonbaf71d32014-10-08 13:00:20 -0700439 Occlusion occlusion_in_quad_space =
440 occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
441 quad_content_to_delegated_target_space);
442
James Robinson646469d2014-10-03 15:33:28 -0700443 gfx::Rect quad_visible_rect =
James Robinsonbaf71d32014-10-08 13:00:20 -0700444 occlusion_in_quad_space.GetUnoccludedContentRect(
James Robinson7f480212014-10-31 10:28:08 -0700445 delegated_quad->visible_rect);
James Robinson646469d2014-10-03 15:33:28 -0700446
447 if (quad_visible_rect.IsEmpty())
448 continue;
449
James Robinson7f480212014-10-31 10:28:08 -0700450 if (delegated_quad->material != DrawQuad::RENDER_PASS) {
James Robinson646469d2014-10-03 15:33:28 -0700451 DrawQuad* output_quad = render_pass->CopyFromAndAppendDrawQuad(
James Robinson7f480212014-10-31 10:28:08 -0700452 delegated_quad, output_shared_quad_state);
James Robinson646469d2014-10-03 15:33:28 -0700453 output_quad->visible_rect = quad_visible_rect;
454 } else {
455 RenderPassId delegated_contributing_render_pass_id =
James Robinson7f480212014-10-31 10:28:08 -0700456 RenderPassDrawQuad::MaterialCast(delegated_quad)->render_pass_id;
James Robinson646469d2014-10-03 15:33:28 -0700457 RenderPassId output_contributing_render_pass_id(-1, -1);
458
459 bool present =
460 ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id,
461 &output_contributing_render_pass_id);
462
463 // The frame may have a RenderPassDrawQuad that points to a RenderPass not
464 // part of the frame. Just ignore these quads.
465 if (present) {
Benjamin Lermancdfc88d2015-02-03 14:35:12 +0100466 DCHECK(output_contributing_render_pass_id != render_pass->id);
James Robinson646469d2014-10-03 15:33:28 -0700467
468 RenderPassDrawQuad* output_quad =
469 render_pass->CopyFromAndAppendRenderPassDrawQuad(
James Robinson7f480212014-10-31 10:28:08 -0700470 RenderPassDrawQuad::MaterialCast(delegated_quad),
James Robinson646469d2014-10-03 15:33:28 -0700471 output_shared_quad_state,
472 output_contributing_render_pass_id);
473 output_quad->visible_rect = quad_visible_rect;
474 }
475 }
476 }
477}
478
479const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
480 return "cc::DelegatedRendererLayerImpl";
481}
482
483void DelegatedRendererLayerImpl::ClearChildId() {
484 if (!child_id_)
485 return;
486
487 if (own_child_id_) {
488 ResourceProvider* provider = layer_tree_impl()->resource_provider();
489 provider->DestroyChild(child_id_);
490 }
491
492 resources_.clear();
493 child_id_ = 0;
494}
495
496} // namespace cc