Home | History | Annotate | Download | only in layers
      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.
      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/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"
     20 
     21 namespace cc {
     22 
     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) {
     29 }
     30 
     31 DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
     32   ClearRenderPasses();
     33   ClearChildId();
     34 }
     35 
     36 bool DelegatedRendererLayerImpl::HasDelegatedContent() const {
     37   return !render_passes_in_draw_order_.empty();
     38 }
     39 
     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;
     45 }
     46 
     47 static ResourceProvider::ResourceId ResourceRemapHelper(
     48     bool* invalid_frame,
     49     const ResourceProvider::ResourceIdMap& child_to_parent_map,
     50     ResourceProvider::ResourceIdSet *remapped_resources,
     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   remapped_resources->insert(remapped_id);
     63   return remapped_id;
     64 }
     65 
     66 void 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->child_id_ = child_id_;
     77   delegated_layer->own_child_id_ = true;
     78   own_child_id_ = false;
     79 
     80   delegated_layer->SetDisplaySize(display_size_);
     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 we keep the data on the pending layer
     89   // for returning resources to the child for now.
     90   delegated_layer->resources_ = resources_;
     91 }
     92 
     93 void DelegatedRendererLayerImpl::SetFrameData(
     94     scoped_ptr<DelegatedFrameData> frame_data,
     95     gfx::RectF damage_in_frame) {
     96   DCHECK(frame_data);
     97 
     98   // A frame with an empty root render pass is invalid.
     99   DCHECK(frame_data->render_pass_list.empty() ||
    100          !frame_data->render_pass_list.back()->output_rect.IsEmpty());
    101 
    102   CreateChildIdIfNeeded();
    103   DCHECK(child_id_);
    104 
    105   ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
    106     const ResourceProvider::ResourceIdMap& resource_map =
    107         resource_provider->GetChildToParentMap(child_id_);
    108 
    109   resource_provider->ReceiveFromChild(child_id_, frame_data->resource_list);
    110 
    111   bool invalid_frame = false;
    112   ResourceProvider::ResourceIdSet used_resources;
    113   DrawQuad::ResourceIteratorCallback remap_resources_to_parent_callback =
    114       base::Bind(&ResourceRemapHelper,
    115                  &invalid_frame,
    116                  resource_map,
    117                  &used_resources);
    118   for (size_t i = 0; i < frame_data->render_pass_list.size(); ++i) {
    119     RenderPass* pass = frame_data->render_pass_list[i];
    120     for (size_t j = 0; j < pass->quad_list.size(); ++j) {
    121       DrawQuad* quad = pass->quad_list[j];
    122       quad->IterateResources(remap_resources_to_parent_callback);
    123     }
    124   }
    125 
    126   if (invalid_frame)
    127     return;
    128 
    129   // Display size is already set so we can compute what the damage rect
    130   // will be in layer space.
    131   if (!frame_data->render_pass_list.empty()) {
    132     RenderPass* new_root_pass = frame_data->render_pass_list.back();
    133     gfx::RectF damage_in_layer = MathUtil::MapClippedRect(
    134         DelegatedFrameToLayerSpaceTransform(
    135             new_root_pass->output_rect.size()),
    136         damage_in_frame);
    137     set_update_rect(gfx::UnionRects(update_rect(), damage_in_layer));
    138   }
    139 
    140   // Save the remapped quads on the layer. This steals the quads and render
    141   // passes from the frame_data.
    142   SetRenderPasses(&frame_data->render_pass_list);
    143   resources_.swap(used_resources);
    144   have_render_passes_to_push_ = true;
    145 }
    146 
    147 void DelegatedRendererLayerImpl::CollectUnusedResources(
    148     TransferableResourceArray* resources_for_ack) {
    149   CreateChildIdIfNeeded();
    150   DCHECK(child_id_);
    151 
    152   ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
    153     const ResourceProvider::ResourceIdMap& resource_map =
    154         resource_provider->GetChildToParentMap(child_id_);
    155 
    156   ResourceProvider::ResourceIdArray unused_resources;
    157   for (ResourceProvider::ResourceIdMap::const_iterator it =
    158            resource_map.begin();
    159        it != resource_map.end();
    160        ++it) {
    161     bool resource_is_in_current_frame = resources_.count(it->second) > 0;
    162     bool resource_is_in_use = resource_provider->InUseByConsumer(it->second);
    163     if (!resource_is_in_current_frame && !resource_is_in_use)
    164       unused_resources.push_back(it->second);
    165   }
    166   resource_provider->PrepareSendToChild(
    167       child_id_, unused_resources, resources_for_ack);
    168 }
    169 
    170 void DelegatedRendererLayerImpl::SetDisplaySize(gfx::Size size) {
    171   if (display_size_ == size)
    172     return;
    173   display_size_ = size;
    174   NoteLayerPropertyChanged();
    175 }
    176 
    177 void DelegatedRendererLayerImpl::SetRenderPasses(
    178     ScopedPtrVector<RenderPass>* render_passes_in_draw_order) {
    179   gfx::RectF old_root_damage;
    180   if (!render_passes_in_draw_order_.empty())
    181     old_root_damage = render_passes_in_draw_order_.back()->damage_rect;
    182 
    183   ClearRenderPasses();
    184 
    185   for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
    186     ScopedPtrVector<RenderPass>::iterator to_take =
    187         render_passes_in_draw_order->begin() + i;
    188     render_passes_index_by_id_.insert(
    189         std::pair<RenderPass::Id, int>((*to_take)->id, i));
    190     scoped_ptr<RenderPass> taken_render_pass =
    191         render_passes_in_draw_order->take(to_take);
    192     render_passes_in_draw_order_.push_back(taken_render_pass.Pass());
    193   }
    194 
    195   if (!render_passes_in_draw_order_.empty())
    196     render_passes_in_draw_order_.back()->damage_rect.Union(old_root_damage);
    197 
    198   // Give back an empty array instead of nulls.
    199   render_passes_in_draw_order->clear();
    200 }
    201 
    202 void DelegatedRendererLayerImpl::ClearRenderPasses() {
    203   // TODO(danakj): Release the resources back to the nested compositor.
    204   render_passes_index_by_id_.clear();
    205   render_passes_in_draw_order_.clear();
    206 }
    207 
    208 scoped_ptr<LayerImpl> DelegatedRendererLayerImpl::CreateLayerImpl(
    209     LayerTreeImpl* tree_impl) {
    210   return DelegatedRendererLayerImpl::Create(
    211       tree_impl, id()).PassAs<LayerImpl>();
    212 }
    213 
    214 void DelegatedRendererLayerImpl::DidLoseOutputSurface() {
    215   ClearRenderPasses();
    216   ClearChildId();
    217 }
    218 
    219 gfx::Transform DelegatedRendererLayerImpl::DelegatedFrameToLayerSpaceTransform(
    220     gfx::Size frame_size) const {
    221   gfx::Size display_size = display_size_.IsEmpty() ? bounds() : display_size_;
    222 
    223   gfx::Transform delegated_frame_to_layer_space_transform;
    224   delegated_frame_to_layer_space_transform.Scale(
    225       static_cast<double>(display_size.width()) / frame_size.width(),
    226       static_cast<double>(display_size.height()) / frame_size.height());
    227   return delegated_frame_to_layer_space_transform;
    228 }
    229 
    230 static inline int IndexToId(int index) { return index + 1; }
    231 static inline int IdToIndex(int id) { return id - 1; }
    232 
    233 RenderPass::Id DelegatedRendererLayerImpl::FirstContributingRenderPassId()
    234     const {
    235   return RenderPass::Id(id(), IndexToId(0));
    236 }
    237 
    238 RenderPass::Id DelegatedRendererLayerImpl::NextContributingRenderPassId(
    239     RenderPass::Id previous) const {
    240   return RenderPass::Id(previous.layer_id, previous.index + 1);
    241 }
    242 
    243 RenderPass::Id DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
    244     RenderPass::Id delegated_render_pass_id) const {
    245   base::hash_map<RenderPass::Id, int>::const_iterator found =
    246       render_passes_index_by_id_.find(delegated_render_pass_id);
    247   DCHECK(found != render_passes_index_by_id_.end());
    248   unsigned delegated_render_pass_index = found->second;
    249   return RenderPass::Id(id(), IndexToId(delegated_render_pass_index));
    250 }
    251 
    252 void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
    253     RenderPassSink* render_pass_sink) {
    254   DCHECK(HasContributingDelegatedRenderPasses());
    255 
    256   for (size_t i = 0; i < render_passes_in_draw_order_.size() - 1; ++i) {
    257     RenderPass::Id output_render_pass_id =
    258         ConvertDelegatedRenderPassId(render_passes_in_draw_order_[i]->id);
    259 
    260     // Don't clash with the RenderPass we generate if we own a RenderSurface.
    261     DCHECK_GT(output_render_pass_id.index, 0);
    262 
    263     render_pass_sink->AppendRenderPass(
    264         render_passes_in_draw_order_[i]->Copy(output_render_pass_id));
    265   }
    266 }
    267 
    268 bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode,
    269                                           ResourceProvider* resource_provider) {
    270   if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
    271     return false;
    272   return LayerImpl::WillDraw(draw_mode, resource_provider);
    273 }
    274 
    275 void DelegatedRendererLayerImpl::AppendQuads(
    276     QuadSink* quad_sink,
    277     AppendQuadsData* append_quads_data) {
    278   AppendRainbowDebugBorder(quad_sink, append_quads_data);
    279 
    280   if (render_passes_in_draw_order_.empty())
    281     return;
    282 
    283   RenderPass::Id target_render_pass_id = append_quads_data->render_pass_id;
    284 
    285   const RenderPass* root_delegated_render_pass =
    286       render_passes_in_draw_order_.back();
    287 
    288   DCHECK(root_delegated_render_pass->output_rect.origin().IsOrigin());
    289   gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
    290 
    291   // If the index of the EenderPassId is 0, then it is a RenderPass generated
    292   // for a layer in this compositor, not the delegated renderer. Then we want to
    293   // merge our root RenderPass with the target RenderPass. Otherwise, it is some
    294   // RenderPass which we added from the delegated renderer.
    295   bool should_merge_root_render_pass_with_target = !target_render_pass_id.index;
    296   if (should_merge_root_render_pass_with_target) {
    297     // Verify that the RenderPass we are appending to is created our
    298     // render_target.
    299     DCHECK(target_render_pass_id.layer_id == render_target()->id());
    300 
    301     AppendRenderPassQuads(
    302         quad_sink, append_quads_data, root_delegated_render_pass, frame_size);
    303   } else {
    304     // Verify that the RenderPass we are appending to was created by us.
    305     DCHECK(target_render_pass_id.layer_id == id());
    306 
    307     int render_pass_index = IdToIndex(target_render_pass_id.index);
    308     const RenderPass* delegated_render_pass =
    309         render_passes_in_draw_order_[render_pass_index];
    310     AppendRenderPassQuads(
    311         quad_sink, append_quads_data, delegated_render_pass, frame_size);
    312   }
    313 }
    314 
    315 void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
    316     QuadSink* quad_sink,
    317     AppendQuadsData* append_quads_data) {
    318   if (!ShowDebugBorders())
    319     return;
    320 
    321   SharedQuadState* shared_quad_state =
    322       quad_sink->UseSharedQuadState(CreateSharedQuadState());
    323 
    324   SkColor color;
    325   float border_width;
    326   GetDebugBorderProperties(&color, &border_width);
    327 
    328   SkColor colors[] = {
    329     0x80ff0000,  // Red.
    330     0x80ffa500,  // Orange.
    331     0x80ffff00,  // Yellow.
    332     0x80008000,  // Green.
    333     0x800000ff,  // Blue.
    334     0x80ee82ee,  // Violet.
    335   };
    336   const int kNumColors = arraysize(colors);
    337 
    338   const int kStripeWidth = 300;
    339   const int kStripeHeight = 300;
    340 
    341   for (size_t i = 0; ; ++i) {
    342     // For horizontal lines.
    343     int x =  kStripeWidth * i;
    344     int width = std::min(kStripeWidth, content_bounds().width() - x - 1);
    345 
    346     // For vertical lines.
    347     int y = kStripeHeight * i;
    348     int height = std::min(kStripeHeight, content_bounds().height() - y - 1);
    349 
    350     gfx::Rect top(x, 0, width, border_width);
    351     gfx::Rect bottom(x,
    352                      content_bounds().height() - border_width,
    353                      width,
    354                      border_width);
    355     gfx::Rect left(0, y, border_width, height);
    356     gfx::Rect right(content_bounds().width() - border_width,
    357                     y,
    358                     border_width,
    359                     height);
    360 
    361     if (top.IsEmpty() && left.IsEmpty())
    362       break;
    363 
    364     if (!top.IsEmpty()) {
    365       scoped_ptr<SolidColorDrawQuad> top_quad = SolidColorDrawQuad::Create();
    366       top_quad->SetNew(shared_quad_state, top, colors[i % kNumColors], false);
    367       quad_sink->Append(top_quad.PassAs<DrawQuad>(), append_quads_data);
    368 
    369       scoped_ptr<SolidColorDrawQuad> bottom_quad = SolidColorDrawQuad::Create();
    370       bottom_quad->SetNew(shared_quad_state,
    371                           bottom,
    372                           colors[kNumColors - 1 - (i % kNumColors)],
    373                           false);
    374       quad_sink->Append(bottom_quad.PassAs<DrawQuad>(), append_quads_data);
    375     }
    376     if (!left.IsEmpty()) {
    377       scoped_ptr<SolidColorDrawQuad> left_quad = SolidColorDrawQuad::Create();
    378       left_quad->SetNew(shared_quad_state,
    379                         left,
    380                         colors[kNumColors - 1 - (i % kNumColors)],
    381                         false);
    382       quad_sink->Append(left_quad.PassAs<DrawQuad>(), append_quads_data);
    383 
    384       scoped_ptr<SolidColorDrawQuad> right_quad = SolidColorDrawQuad::Create();
    385       right_quad->SetNew(
    386           shared_quad_state, right, colors[i % kNumColors], false);
    387       quad_sink->Append(right_quad.PassAs<DrawQuad>(), append_quads_data);
    388     }
    389   }
    390 }
    391 
    392 void DelegatedRendererLayerImpl::AppendRenderPassQuads(
    393     QuadSink* quad_sink,
    394     AppendQuadsData* append_quads_data,
    395     const RenderPass* delegated_render_pass,
    396     gfx::Size frame_size) const {
    397 
    398   const SharedQuadState* delegated_shared_quad_state = NULL;
    399   SharedQuadState* output_shared_quad_state = NULL;
    400 
    401   for (size_t i = 0; i < delegated_render_pass->quad_list.size(); ++i) {
    402     const DrawQuad* delegated_quad = delegated_render_pass->quad_list[i];
    403 
    404     if (delegated_quad->shared_quad_state != delegated_shared_quad_state) {
    405       delegated_shared_quad_state = delegated_quad->shared_quad_state;
    406       output_shared_quad_state = quad_sink->UseSharedQuadState(
    407           delegated_shared_quad_state->Copy());
    408 
    409       bool is_root_delegated_render_pass =
    410           delegated_render_pass == render_passes_in_draw_order_.back();
    411       if (is_root_delegated_render_pass) {
    412         // Don't allow areas inside the bounds that are empty.
    413         DCHECK(display_size_.IsEmpty() ||
    414                gfx::Rect(display_size_).Contains(gfx::Rect(bounds())));
    415         gfx::Transform delegated_frame_to_target_transform =
    416             draw_transform() * DelegatedFrameToLayerSpaceTransform(frame_size);
    417 
    418         output_shared_quad_state->content_to_target_transform.ConcatTransform(
    419             delegated_frame_to_target_transform);
    420 
    421         if (render_target() == this) {
    422           DCHECK(!is_clipped());
    423           DCHECK(render_surface());
    424           output_shared_quad_state->clip_rect = MathUtil::MapClippedRect(
    425               delegated_frame_to_target_transform,
    426               output_shared_quad_state->clip_rect);
    427         } else {
    428           gfx::Rect clip_rect = drawable_content_rect();
    429           if (output_shared_quad_state->is_clipped) {
    430             clip_rect.Intersect(MathUtil::MapClippedRect(
    431                 delegated_frame_to_target_transform,
    432                 output_shared_quad_state->clip_rect));
    433           }
    434           output_shared_quad_state->clip_rect = clip_rect;
    435           output_shared_quad_state->is_clipped = true;
    436         }
    437 
    438         output_shared_quad_state->opacity *= draw_opacity();
    439       }
    440     }
    441     DCHECK(output_shared_quad_state);
    442 
    443     scoped_ptr<DrawQuad> output_quad;
    444     if (delegated_quad->material != DrawQuad::RENDER_PASS) {
    445       output_quad = delegated_quad->Copy(output_shared_quad_state);
    446     } else {
    447       RenderPass::Id delegated_contributing_render_pass_id =
    448           RenderPassDrawQuad::MaterialCast(delegated_quad)->render_pass_id;
    449       RenderPass::Id output_contributing_render_pass_id =
    450           ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id);
    451       DCHECK(output_contributing_render_pass_id !=
    452              append_quads_data->render_pass_id);
    453 
    454       output_quad = RenderPassDrawQuad::MaterialCast(delegated_quad)->Copy(
    455           output_shared_quad_state,
    456           output_contributing_render_pass_id).PassAs<DrawQuad>();
    457     }
    458     DCHECK(output_quad.get());
    459 
    460     quad_sink->Append(output_quad.Pass(), append_quads_data);
    461   }
    462 }
    463 
    464 const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
    465   return "cc::DelegatedRendererLayerImpl";
    466 }
    467 
    468 void DelegatedRendererLayerImpl::CreateChildIdIfNeeded() {
    469   if (child_id_)
    470     return;
    471 
    472   ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
    473   child_id_ = resource_provider->CreateChild();
    474   own_child_id_ = true;
    475 }
    476 
    477 void DelegatedRendererLayerImpl::ClearChildId() {
    478   if (!child_id_)
    479     return;
    480 
    481   if (own_child_id_) {
    482     ResourceProvider* provider = layer_tree_impl()->resource_provider();
    483     provider->DestroyChild(child_id_);
    484   }
    485 
    486   child_id_ = 0;
    487 }
    488 
    489 }  // namespace cc
    490