Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / cc / layers / delegated_renderer_layer_impl.cc
blob9b3a513206c17431d8ce0e110f8d2b2da940702b
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/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.h"
20 #include "ui/gfx/geometry/rect_conversions.h"
22 namespace cc {
24 DelegatedRendererLayerImpl::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) {
33 DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
34 ClearRenderPasses();
35 ClearChildId();
38 bool DelegatedRendererLayerImpl::HasDelegatedContent() const { return true; }
40 bool 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;
47 void DelegatedRendererLayerImpl::PushPropertiesTo(LayerImpl* layer) {
48 LayerImpl::PushPropertiesTo(layer);
50 DelegatedRendererLayerImpl* delegated_layer =
51 static_cast<DelegatedRendererLayerImpl*>(layer);
53 // If we have a new child_id to give to the active layer, it should
54 // have already deleted its old child_id.
55 DCHECK(delegated_layer->child_id_ == 0 ||
56 delegated_layer->child_id_ == child_id_);
57 delegated_layer->inverse_device_scale_factor_ = inverse_device_scale_factor_;
58 delegated_layer->child_id_ = child_id_;
59 delegated_layer->own_child_id_ = true;
60 own_child_id_ = false;
62 if (have_render_passes_to_push_) {
63 DCHECK(child_id_);
64 // This passes ownership of the render passes to the active tree.
65 delegated_layer->SetRenderPasses(&render_passes_in_draw_order_);
66 // Once resources are on the active tree, give them to the ResourceProvider
67 // and release unused resources from the old frame.
68 delegated_layer->TakeOwnershipOfResourcesIfOnActiveTree(resources_);
69 DCHECK(render_passes_in_draw_order_.empty());
70 have_render_passes_to_push_ = false;
73 // This is just a copy for testing, since resources are added to the
74 // ResourceProvider in the pending tree.
75 delegated_layer->resources_ = resources_;
78 void DelegatedRendererLayerImpl::CreateChildIdIfNeeded(
79 const ReturnCallback& return_callback) {
80 if (child_id_)
81 return;
83 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
84 child_id_ = resource_provider->CreateChild(return_callback);
85 own_child_id_ = true;
88 void DelegatedRendererLayerImpl::SetFrameData(
89 const DelegatedFrameData* frame_data,
90 const gfx::Rect& damage_in_frame) {
91 DCHECK(child_id_) << "CreateChildIdIfNeeded must be called first.";
92 DCHECK(frame_data);
93 DCHECK(!frame_data->render_pass_list.empty());
94 // A frame with an empty root render pass is invalid.
95 DCHECK(!frame_data->render_pass_list.back()->output_rect.IsEmpty());
97 ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
98 const ResourceProvider::ResourceIdMap& resource_map =
99 resource_provider->GetChildToParentMap(child_id_);
101 resource_provider->ReceiveFromChild(child_id_, frame_data->resource_list);
103 RenderPassList render_pass_list;
104 RenderPass::CopyAll(frame_data->render_pass_list, &render_pass_list);
106 bool invalid_frame = false;
107 ResourceProvider::ResourceIdSet resources_in_frame;
108 size_t reserve_size = frame_data->resource_list.size();
109 #if defined(COMPILER_MSVC)
110 resources_in_frame.reserve(reserve_size);
111 #elif defined(COMPILER_GCC)
112 // Pre-standard hash-tables only implement resize, which behaves similarly
113 // to reserve for these keys. Resizing to 0 may also be broken (particularly
114 // on stlport).
115 // TODO(jbauman): Replace with reserve when C++11 is supported everywhere.
116 if (reserve_size)
117 resources_in_frame.resize(reserve_size);
118 #endif
119 for (const auto& pass : render_pass_list) {
120 for (const auto& quad : pass->quad_list) {
121 for (ResourceId& resource_id : quad->resources) {
122 ResourceProvider::ResourceIdMap::const_iterator it =
123 resource_map.find(resource_id);
124 if (it == resource_map.end()) {
125 invalid_frame = true;
126 break;
129 DCHECK_EQ(it->first, resource_id);
130 ResourceId remapped_id = it->second;
131 resources_in_frame.insert(resource_id);
132 resource_id = remapped_id;
137 if (invalid_frame) {
138 // Declare we are still using the last frame's resources. Drops ownership of
139 // any invalid resources, keeping only those in use by the active tree.
140 resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
141 return;
144 // Save the new frame's resources, but don't give them to the ResourceProvider
145 // until they are active, since the resources on the active tree will still be
146 // used and we don't want to return them early.
147 resources_.swap(resources_in_frame);
148 TakeOwnershipOfResourcesIfOnActiveTree(resources_);
150 inverse_device_scale_factor_ = 1.0f / frame_data->device_scale_factor;
151 // Display size is already set so we can compute what the damage rect
152 // will be in layer space. The damage may exceed the visible portion of
153 // the frame, so intersect the damage to the layer's bounds.
154 RenderPass* new_root_pass = render_pass_list.back();
155 gfx::Size frame_size = new_root_pass->output_rect.size();
156 gfx::Rect damage_in_layer =
157 gfx::ScaleToEnclosingRect(damage_in_frame, inverse_device_scale_factor_);
158 SetUpdateRect(gfx::IntersectRects(
159 gfx::UnionRects(update_rect(), damage_in_layer), gfx::Rect(bounds())));
161 SetRenderPasses(&render_pass_list);
162 have_render_passes_to_push_ = true;
165 void DelegatedRendererLayerImpl::TakeOwnershipOfResourcesIfOnActiveTree(
166 const ResourceProvider::ResourceIdSet& resources) {
167 DCHECK(child_id_);
168 if (!layer_tree_impl()->IsActiveTree())
169 return;
170 layer_tree_impl()->resource_provider()->DeclareUsedResourcesFromChild(
171 child_id_, resources);
174 void DelegatedRendererLayerImpl::SetRenderPasses(
175 RenderPassList* render_passes_in_draw_order) {
176 ClearRenderPasses();
178 for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
179 RenderPassList::iterator to_take =
180 render_passes_in_draw_order->begin() + i;
181 render_passes_index_by_id_.insert(
182 RenderPassToIndexMap::value_type((*to_take)->id, i));
183 scoped_ptr<RenderPass> taken_render_pass =
184 render_passes_in_draw_order->take(to_take);
185 render_passes_in_draw_order_.push_back(taken_render_pass.Pass());
188 // Give back an empty array instead of nulls.
189 render_passes_in_draw_order->clear();
191 // The render passes given here become part of the RenderSurfaceLayerList, so
192 // changing them requires recomputing the RenderSurfaceLayerList.
193 layer_tree_impl()->set_needs_update_draw_properties();
196 void DelegatedRendererLayerImpl::ClearRenderPasses() {
197 render_passes_index_by_id_.clear();
198 render_passes_in_draw_order_.clear();
201 scoped_ptr<LayerImpl> DelegatedRendererLayerImpl::CreateLayerImpl(
202 LayerTreeImpl* tree_impl) {
203 return DelegatedRendererLayerImpl::Create(tree_impl, id());
206 void DelegatedRendererLayerImpl::ReleaseResources() {
207 ClearRenderPasses();
208 ClearChildId();
209 have_render_passes_to_push_ = false;
212 static inline size_t IndexToId(size_t index) {
213 return index + 1;
215 static inline size_t IdToIndex(size_t id) {
216 DCHECK_GT(id, 0u);
217 return id - 1;
220 RenderPassId DelegatedRendererLayerImpl::FirstContributingRenderPassId() const {
221 return RenderPassId(id(), IndexToId(0));
224 RenderPassId DelegatedRendererLayerImpl::NextContributingRenderPassId(
225 RenderPassId previous) const {
226 return RenderPassId(previous.layer_id, previous.index + 1);
229 bool DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
230 RenderPassId delegated_render_pass_id,
231 RenderPassId* output_render_pass_id) const {
232 RenderPassToIndexMap::const_iterator found =
233 render_passes_index_by_id_.find(delegated_render_pass_id);
234 if (found == render_passes_index_by_id_.end()) {
235 // Be robust against a RenderPass id that isn't part of the frame.
236 return false;
238 size_t delegated_render_pass_index = found->second;
239 *output_render_pass_id =
240 RenderPassId(id(), IndexToId(delegated_render_pass_index));
241 return true;
244 void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
245 RenderPassSink* render_pass_sink) {
246 DCHECK(HasContributingDelegatedRenderPasses());
248 const RenderPass* root_delegated_render_pass =
249 render_passes_in_draw_order_.back();
250 gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
251 gfx::Transform delegated_frame_to_root_transform = screen_space_transform();
252 delegated_frame_to_root_transform.Scale(inverse_device_scale_factor_,
253 inverse_device_scale_factor_);
255 for (size_t i = 0; i < render_passes_in_draw_order_.size() - 1; ++i) {
256 RenderPassId output_render_pass_id;
257 bool present =
258 ConvertDelegatedRenderPassId(render_passes_in_draw_order_[i]->id,
259 &output_render_pass_id);
261 // Don't clash with the RenderPass we generate if we own a RenderSurface.
262 DCHECK(present) << render_passes_in_draw_order_[i]->id.layer_id << ", "
263 << render_passes_in_draw_order_[i]->id.index;
264 DCHECK_GT(output_render_pass_id.index, 0u);
266 scoped_ptr<RenderPass> copy_pass =
267 render_passes_in_draw_order_[i]->Copy(output_render_pass_id);
268 copy_pass->transform_to_root_target.ConcatTransform(
269 delegated_frame_to_root_transform);
270 render_pass_sink->AppendRenderPass(copy_pass.Pass());
274 bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode,
275 ResourceProvider* resource_provider) {
276 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
277 return false;
278 return LayerImpl::WillDraw(draw_mode, resource_provider);
281 void DelegatedRendererLayerImpl::AppendQuads(
282 RenderPass* render_pass,
283 AppendQuadsData* append_quads_data) {
284 AppendRainbowDebugBorder(render_pass);
286 // This list will be empty after a lost context until a new frame arrives.
287 if (render_passes_in_draw_order_.empty())
288 return;
290 RenderPassId target_render_pass_id = render_pass->id;
292 const RenderPass* root_delegated_render_pass =
293 render_passes_in_draw_order_.back();
295 DCHECK(root_delegated_render_pass->output_rect.origin().IsOrigin());
296 gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
298 // If the index of the RenderPassId is 0, then it is a RenderPass generated
299 // for a layer in this compositor, not the delegating renderer. Then we want
300 // to merge our root RenderPass with the target RenderPass. Otherwise, it is
301 // some RenderPass which we added from the delegating renderer.
302 bool should_merge_root_render_pass_with_target = !target_render_pass_id.index;
303 if (should_merge_root_render_pass_with_target) {
304 // Verify that the RenderPass we are appending to is created by our
305 // render_target.
306 DCHECK(target_render_pass_id.layer_id == render_target()->id());
308 AppendRenderPassQuads(render_pass,
309 root_delegated_render_pass,
310 frame_size);
311 } else {
312 // Verify that the RenderPass we are appending to was created by us.
313 DCHECK(target_render_pass_id.layer_id == id());
315 size_t render_pass_index = IdToIndex(target_render_pass_id.index);
316 const RenderPass* delegated_render_pass =
317 render_passes_in_draw_order_[render_pass_index];
318 AppendRenderPassQuads(render_pass,
319 delegated_render_pass,
320 frame_size);
324 void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
325 RenderPass* render_pass) {
326 if (!ShowDebugBorders())
327 return;
329 SharedQuadState* shared_quad_state =
330 render_pass->CreateAndAppendSharedQuadState();
331 PopulateSharedQuadState(shared_quad_state);
333 SkColor color;
334 float border_width;
335 GetDebugBorderProperties(&color, &border_width);
337 SkColor colors[] = {
338 0x80ff0000, // Red.
339 0x80ffa500, // Orange.
340 0x80ffff00, // Yellow.
341 0x80008000, // Green.
342 0x800000ff, // Blue.
343 0x80ee82ee, // Violet.
345 const int kNumColors = arraysize(colors);
347 const int kStripeWidth = 300;
348 const int kStripeHeight = 300;
350 for (int i = 0;; ++i) {
351 // For horizontal lines.
352 int x = kStripeWidth * i;
353 int width = std::min(kStripeWidth, bounds().width() - x - 1);
355 // For vertical lines.
356 int y = kStripeHeight * i;
357 int height = std::min(kStripeHeight, bounds().height() - y - 1);
359 gfx::Rect top(x, 0, width, border_width);
360 gfx::Rect bottom(x, bounds().height() - border_width, width, border_width);
361 gfx::Rect left(0, y, border_width, height);
362 gfx::Rect right(bounds().width() - border_width, y, border_width, height);
364 if (top.IsEmpty() && left.IsEmpty())
365 break;
367 if (!top.IsEmpty()) {
368 bool force_anti_aliasing_off = false;
369 SolidColorDrawQuad* top_quad =
370 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
371 top_quad->SetNew(shared_quad_state, top, top, colors[i % kNumColors],
372 force_anti_aliasing_off);
374 SolidColorDrawQuad* bottom_quad =
375 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
376 bottom_quad->SetNew(shared_quad_state, bottom, bottom,
377 colors[kNumColors - 1 - (i % kNumColors)],
378 force_anti_aliasing_off);
380 if (contents_opaque()) {
381 // Draws a stripe filling the layer vertically with the same color and
382 // width as the horizontal stipes along the layer's top border.
383 SolidColorDrawQuad* solid_quad =
384 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
385 // The inner fill is more transparent then the border.
386 static const float kFillOpacity = 0.1f;
387 SkColor fill_color = SkColorSetA(
388 colors[i % kNumColors],
389 static_cast<uint8_t>(SkColorGetA(colors[i % kNumColors]) *
390 kFillOpacity));
391 gfx::Rect fill_rect(x, 0, width, bounds().height());
392 solid_quad->SetNew(shared_quad_state, fill_rect, fill_rect, fill_color,
393 force_anti_aliasing_off);
396 if (!left.IsEmpty()) {
397 bool force_anti_aliasing_off = false;
398 SolidColorDrawQuad* left_quad =
399 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
400 left_quad->SetNew(shared_quad_state, left, left,
401 colors[kNumColors - 1 - (i % kNumColors)],
402 force_anti_aliasing_off);
404 SolidColorDrawQuad* right_quad =
405 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
406 right_quad->SetNew(shared_quad_state, right, right,
407 colors[i % kNumColors], force_anti_aliasing_off);
412 void DelegatedRendererLayerImpl::AppendRenderPassQuads(
413 RenderPass* render_pass,
414 const RenderPass* delegated_render_pass,
415 const gfx::Size& frame_size) const {
416 const SharedQuadState* delegated_shared_quad_state = nullptr;
417 SharedQuadState* output_shared_quad_state = nullptr;
419 gfx::Transform delegated_frame_to_target_transform = draw_transform();
420 delegated_frame_to_target_transform.Scale(inverse_device_scale_factor_,
421 inverse_device_scale_factor_);
422 bool is_root_delegated_render_pass =
423 delegated_render_pass == render_passes_in_draw_order_.back();
424 for (const auto& delegated_quad : delegated_render_pass->quad_list) {
425 if (delegated_quad->shared_quad_state != delegated_shared_quad_state) {
426 delegated_shared_quad_state = delegated_quad->shared_quad_state;
427 output_shared_quad_state = render_pass->CreateAndAppendSharedQuadState();
428 output_shared_quad_state->CopyFrom(delegated_shared_quad_state);
430 if (is_root_delegated_render_pass) {
431 output_shared_quad_state->quad_to_target_transform.ConcatTransform(
432 delegated_frame_to_target_transform);
434 if (render_target() == this) {
435 DCHECK(!is_clipped());
436 DCHECK(render_surface());
437 DCHECK_EQ(0u, num_unclipped_descendants());
438 output_shared_quad_state->clip_rect =
439 MathUtil::MapEnclosingClippedRect(
440 delegated_frame_to_target_transform,
441 output_shared_quad_state->clip_rect);
442 } else {
443 gfx::Rect clip_rect = drawable_content_rect();
444 if (output_shared_quad_state->is_clipped) {
445 clip_rect.Intersect(MathUtil::MapEnclosingClippedRect(
446 delegated_frame_to_target_transform,
447 output_shared_quad_state->clip_rect));
449 output_shared_quad_state->clip_rect = clip_rect;
450 output_shared_quad_state->is_clipped = true;
453 output_shared_quad_state->opacity *= draw_opacity();
456 DCHECK(output_shared_quad_state);
458 gfx::Transform quad_content_to_delegated_target_space =
459 output_shared_quad_state->quad_to_target_transform;
460 if (!is_root_delegated_render_pass) {
461 quad_content_to_delegated_target_space.ConcatTransform(
462 delegated_render_pass->transform_to_root_target);
463 quad_content_to_delegated_target_space.ConcatTransform(
464 delegated_frame_to_target_transform);
467 Occlusion occlusion_in_quad_space =
468 draw_properties()
469 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
470 quad_content_to_delegated_target_space);
472 gfx::Rect quad_visible_rect =
473 occlusion_in_quad_space.GetUnoccludedContentRect(
474 delegated_quad->visible_rect);
476 if (quad_visible_rect.IsEmpty())
477 continue;
479 if (delegated_quad->material != DrawQuad::RENDER_PASS) {
480 DrawQuad* output_quad = render_pass->CopyFromAndAppendDrawQuad(
481 delegated_quad, output_shared_quad_state);
482 output_quad->visible_rect = quad_visible_rect;
483 ValidateQuadResources(output_quad);
484 } else {
485 RenderPassId delegated_contributing_render_pass_id =
486 RenderPassDrawQuad::MaterialCast(delegated_quad)->render_pass_id;
487 RenderPassId output_contributing_render_pass_id;
489 bool present =
490 ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id,
491 &output_contributing_render_pass_id);
492 // |present| being false means the child compositor sent an invalid frame.
493 DCHECK(present);
494 DCHECK(output_contributing_render_pass_id != render_pass->id);
496 RenderPassDrawQuad* output_quad =
497 render_pass->CopyFromAndAppendRenderPassDrawQuad(
498 RenderPassDrawQuad::MaterialCast(delegated_quad),
499 output_shared_quad_state, output_contributing_render_pass_id);
500 output_quad->visible_rect = quad_visible_rect;
501 ValidateQuadResources(output_quad);
506 const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
507 return "cc::DelegatedRendererLayerImpl";
510 void DelegatedRendererLayerImpl::ClearChildId() {
511 if (!child_id_)
512 return;
514 if (own_child_id_) {
515 ResourceProvider* provider = layer_tree_impl()->resource_provider();
516 provider->DestroyChild(child_id_);
519 resources_.clear();
520 child_id_ = 0;
523 } // namespace cc