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/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"
     20 
     21 namespace cc {
     22 
     23 DelegatedRendererLayerImpl::DelegatedRendererLayerImpl(LayerTreeImpl* tree_impl,
     24                                                        int id)
     25     : LayerImpl(tree_impl, id),
     26       have_render_passes_to_push_(false),
     27       inverse_device_scale_factor_(1.0f),
     28       child_id_(0),
     29       own_child_id_(false) {
     30 }
     31 
     32 DelegatedRendererLayerImpl::~DelegatedRendererLayerImpl() {
     33   ClearRenderPasses();
     34   ClearChildId();
     35 }
     36 
     37 bool DelegatedRendererLayerImpl::HasDelegatedContent() const { return true; }
     38 
     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;
     44 }
     45 
     46 static ResourceProvider::ResourceId ResourceRemapHelper(
     47     bool* invalid_frame,
     48     const ResourceProvider::ResourceIdMap& child_to_parent_map,
     49     ResourceProvider::ResourceIdArray* resources_in_frame,
     50     ResourceProvider::ResourceId id) {
     51 
     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;
     56     return 0;
     57   }
     58 
     59   DCHECK_EQ(it->first, id);
     60   ResourceProvider::ResourceId remapped_id = it->second;
     61   resources_in_frame->push_back(id);
     62   return remapped_id;
     63 }
     64 
     65 void DelegatedRendererLayerImpl::PushPropertiesTo(LayerImpl* layer) {
     66   LayerImpl::PushPropertiesTo(layer);
     67 
     68   DelegatedRendererLayerImpl* delegated_layer =
     69       static_cast<DelegatedRendererLayerImpl*>(layer);
     70 
     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;
     79 
     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;
     85   }
     86 
     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_;
     90 }
     91 
     92 void DelegatedRendererLayerImpl::CreateChildIdIfNeeded(
     93     const ReturnCallback& return_callback) {
     94   if (child_id_)
     95     return;
     96 
     97   ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
     98   child_id_ = resource_provider->CreateChild(return_callback);
     99   own_child_id_ = true;
    100 }
    101 
    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.";
    106   DCHECK(frame_data);
    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());
    110 
    111   ResourceProvider* resource_provider = layer_tree_impl()->resource_provider();
    112     const ResourceProvider::ResourceIdMap& resource_map =
    113         resource_provider->GetChildToParentMap(child_id_);
    114 
    115   resource_provider->ReceiveFromChild(child_id_, frame_data->resource_list);
    116 
    117   RenderPassList render_pass_list;
    118   RenderPass::CopyAll(frame_data->render_pass_list, &render_pass_list);
    119 
    120   bool invalid_frame = false;
    121   ResourceProvider::ResourceIdArray resources_in_frame;
    122   DrawQuad::ResourceIteratorCallback remap_resources_to_parent_callback =
    123       base::Bind(&ResourceRemapHelper,
    124                  &invalid_frame,
    125                  resource_map,
    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 (QuadList::Iterator iter = pass->quad_list.begin();
    130          iter != pass->quad_list.end();
    131          ++iter) {
    132       iter->IterateResources(remap_resources_to_parent_callback);
    133     }
    134   }
    135 
    136   if (invalid_frame) {
    137     // Declare we are still using the last frame's resources.
    138     resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
    139     return;
    140   }
    141 
    142   // Declare we are using the new frame's resources.
    143   resources_.swap(resources_in_frame);
    144   resource_provider->DeclareUsedResourcesFromChild(child_id_, resources_);
    145 
    146   inverse_device_scale_factor_ = 1.0f / frame_data->device_scale_factor;
    147   // Display size is already set so we can compute what the damage rect
    148   // will be in layer space. The damage may exceed the visible portion of
    149   // the frame, so intersect the damage to the layer's bounds.
    150   RenderPass* new_root_pass = render_pass_list.back();
    151   gfx::Size frame_size = new_root_pass->output_rect.size();
    152   gfx::RectF damage_in_layer = damage_in_frame;
    153   damage_in_layer.Scale(inverse_device_scale_factor_);
    154   SetUpdateRect(gfx::IntersectRects(
    155       gfx::UnionRects(update_rect(), damage_in_layer), gfx::Rect(bounds())));
    156 
    157   SetRenderPasses(&render_pass_list);
    158   have_render_passes_to_push_ = true;
    159 }
    160 
    161 void DelegatedRendererLayerImpl::SetRenderPasses(
    162     RenderPassList* render_passes_in_draw_order) {
    163   ClearRenderPasses();
    164 
    165   for (size_t i = 0; i < render_passes_in_draw_order->size(); ++i) {
    166     RenderPassList::iterator to_take =
    167         render_passes_in_draw_order->begin() + i;
    168     render_passes_index_by_id_.insert(
    169         std::pair<RenderPassId, int>((*to_take)->id, i));
    170     scoped_ptr<RenderPass> taken_render_pass =
    171         render_passes_in_draw_order->take(to_take);
    172     render_passes_in_draw_order_.push_back(taken_render_pass.Pass());
    173   }
    174 
    175   // Give back an empty array instead of nulls.
    176   render_passes_in_draw_order->clear();
    177 }
    178 
    179 void DelegatedRendererLayerImpl::ClearRenderPasses() {
    180   render_passes_index_by_id_.clear();
    181   render_passes_in_draw_order_.clear();
    182 }
    183 
    184 scoped_ptr<LayerImpl> DelegatedRendererLayerImpl::CreateLayerImpl(
    185     LayerTreeImpl* tree_impl) {
    186   return DelegatedRendererLayerImpl::Create(
    187       tree_impl, id()).PassAs<LayerImpl>();
    188 }
    189 
    190 void DelegatedRendererLayerImpl::ReleaseResources() {
    191   ClearRenderPasses();
    192   ClearChildId();
    193 }
    194 
    195 static inline int IndexToId(int index) { return index + 1; }
    196 static inline int IdToIndex(int id) { return id - 1; }
    197 
    198 RenderPassId DelegatedRendererLayerImpl::FirstContributingRenderPassId() const {
    199   return RenderPassId(id(), IndexToId(0));
    200 }
    201 
    202 RenderPassId DelegatedRendererLayerImpl::NextContributingRenderPassId(
    203     RenderPassId previous) const {
    204   return RenderPassId(previous.layer_id, previous.index + 1);
    205 }
    206 
    207 bool DelegatedRendererLayerImpl::ConvertDelegatedRenderPassId(
    208     RenderPassId delegated_render_pass_id,
    209     RenderPassId* output_render_pass_id) const {
    210   base::hash_map<RenderPassId, int>::const_iterator found =
    211       render_passes_index_by_id_.find(delegated_render_pass_id);
    212   if (found == render_passes_index_by_id_.end()) {
    213     // Be robust against a RenderPass id that isn't part of the frame.
    214     return false;
    215   }
    216   unsigned delegated_render_pass_index = found->second;
    217   *output_render_pass_id =
    218       RenderPassId(id(), IndexToId(delegated_render_pass_index));
    219   return true;
    220 }
    221 
    222 void DelegatedRendererLayerImpl::AppendContributingRenderPasses(
    223     RenderPassSink* render_pass_sink) {
    224   DCHECK(HasContributingDelegatedRenderPasses());
    225 
    226   const RenderPass* root_delegated_render_pass =
    227       render_passes_in_draw_order_.back();
    228   gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
    229   gfx::Transform delegated_frame_to_root_transform = screen_space_transform();
    230   delegated_frame_to_root_transform.Scale(inverse_device_scale_factor_,
    231                                           inverse_device_scale_factor_);
    232 
    233   for (size_t i = 0; i < render_passes_in_draw_order_.size() - 1; ++i) {
    234     RenderPassId output_render_pass_id(-1, -1);
    235     bool present =
    236         ConvertDelegatedRenderPassId(render_passes_in_draw_order_[i]->id,
    237                                      &output_render_pass_id);
    238 
    239     // Don't clash with the RenderPass we generate if we own a RenderSurface.
    240     DCHECK(present) << render_passes_in_draw_order_[i]->id.layer_id << ", "
    241                     << render_passes_in_draw_order_[i]->id.index;
    242     DCHECK_GT(output_render_pass_id.index, 0);
    243 
    244     scoped_ptr<RenderPass> copy_pass =
    245         render_passes_in_draw_order_[i]->Copy(output_render_pass_id);
    246     copy_pass->transform_to_root_target.ConcatTransform(
    247         delegated_frame_to_root_transform);
    248     render_pass_sink->AppendRenderPass(copy_pass.Pass());
    249   }
    250 }
    251 
    252 bool DelegatedRendererLayerImpl::WillDraw(DrawMode draw_mode,
    253                                           ResourceProvider* resource_provider) {
    254   if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
    255     return false;
    256   return LayerImpl::WillDraw(draw_mode, resource_provider);
    257 }
    258 
    259 void DelegatedRendererLayerImpl::AppendQuads(
    260     RenderPass* render_pass,
    261     const OcclusionTracker<LayerImpl>& occlusion_tracker,
    262     AppendQuadsData* append_quads_data) {
    263   AppendRainbowDebugBorder(render_pass, append_quads_data);
    264 
    265   // This list will be empty after a lost context until a new frame arrives.
    266   if (render_passes_in_draw_order_.empty())
    267     return;
    268 
    269   RenderPassId target_render_pass_id = append_quads_data->render_pass_id;
    270 
    271   const RenderPass* root_delegated_render_pass =
    272       render_passes_in_draw_order_.back();
    273 
    274   DCHECK(root_delegated_render_pass->output_rect.origin().IsOrigin());
    275   gfx::Size frame_size = root_delegated_render_pass->output_rect.size();
    276 
    277   // If the index of the RenderPassId is 0, then it is a RenderPass generated
    278   // for a layer in this compositor, not the delegating renderer. Then we want
    279   // to merge our root RenderPass with the target RenderPass. Otherwise, it is
    280   // some RenderPass which we added from the delegating renderer.
    281   bool should_merge_root_render_pass_with_target = !target_render_pass_id.index;
    282   if (should_merge_root_render_pass_with_target) {
    283     // Verify that the RenderPass we are appending to is created by our
    284     // render_target.
    285     DCHECK(target_render_pass_id.layer_id == render_target()->id());
    286 
    287     AppendRenderPassQuads(render_pass,
    288                           occlusion_tracker,
    289                           append_quads_data,
    290                           root_delegated_render_pass,
    291                           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());
    295 
    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(render_pass,
    300                           occlusion_tracker,
    301                           append_quads_data,
    302                           delegated_render_pass,
    303                           frame_size);
    304   }
    305 }
    306 
    307 void DelegatedRendererLayerImpl::AppendRainbowDebugBorder(
    308     RenderPass* render_pass,
    309     AppendQuadsData* append_quads_data) {
    310   if (!ShowDebugBorders())
    311     return;
    312 
    313   SharedQuadState* shared_quad_state =
    314       render_pass->CreateAndAppendSharedQuadState();
    315   PopulateSharedQuadState(shared_quad_state);
    316 
    317   SkColor color;
    318   float border_width;
    319   GetDebugBorderProperties(&color, &border_width);
    320 
    321   SkColor colors[] = {
    322     0x80ff0000,  // Red.
    323     0x80ffa500,  // Orange.
    324     0x80ffff00,  // Yellow.
    325     0x80008000,  // Green.
    326     0x800000ff,  // Blue.
    327     0x80ee82ee,  // Violet.
    328   };
    329   const int kNumColors = arraysize(colors);
    330 
    331   const int kStripeWidth = 300;
    332   const int kStripeHeight = 300;
    333 
    334   for (size_t i = 0; ; ++i) {
    335     // For horizontal lines.
    336     int x =  kStripeWidth * i;
    337     int width = std::min(kStripeWidth, content_bounds().width() - x - 1);
    338 
    339     // For vertical lines.
    340     int y = kStripeHeight * i;
    341     int height = std::min(kStripeHeight, content_bounds().height() - y - 1);
    342 
    343     gfx::Rect top(x, 0, width, border_width);
    344     gfx::Rect bottom(x,
    345                      content_bounds().height() - border_width,
    346                      width,
    347                      border_width);
    348     gfx::Rect left(0, y, border_width, height);
    349     gfx::Rect right(content_bounds().width() - border_width,
    350                     y,
    351                     border_width,
    352                     height);
    353 
    354     if (top.IsEmpty() && left.IsEmpty())
    355       break;
    356 
    357     if (!top.IsEmpty()) {
    358       SolidColorDrawQuad* top_quad =
    359           render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
    360       top_quad->SetNew(
    361           shared_quad_state, top, top, colors[i % kNumColors], false);
    362 
    363       SolidColorDrawQuad* bottom_quad =
    364           render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
    365       bottom_quad->SetNew(shared_quad_state,
    366                           bottom,
    367                           bottom,
    368                           colors[kNumColors - 1 - (i % kNumColors)],
    369                           false);
    370     }
    371     if (!left.IsEmpty()) {
    372       SolidColorDrawQuad* left_quad =
    373           render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
    374       left_quad->SetNew(shared_quad_state,
    375                         left,
    376                         left,
    377                         colors[kNumColors - 1 - (i % kNumColors)],
    378                         false);
    379 
    380       SolidColorDrawQuad* right_quad =
    381           render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
    382       right_quad->SetNew(
    383           shared_quad_state, right, right, colors[i % kNumColors], false);
    384     }
    385   }
    386 }
    387 
    388 void DelegatedRendererLayerImpl::AppendRenderPassQuads(
    389     RenderPass* render_pass,
    390     const OcclusionTracker<LayerImpl>& occlusion_tracker,
    391     AppendQuadsData* append_quads_data,
    392     const RenderPass* delegated_render_pass,
    393     const gfx::Size& frame_size) const {
    394   const SharedQuadState* delegated_shared_quad_state = NULL;
    395   SharedQuadState* output_shared_quad_state = NULL;
    396 
    397   for (QuadList::ConstIterator iter = delegated_render_pass->quad_list.begin();
    398        iter != delegated_render_pass->quad_list.end();
    399        ++iter) {
    400     const DrawQuad* delegated_quad = &*iter;
    401 
    402     bool is_root_delegated_render_pass =
    403         delegated_render_pass == render_passes_in_draw_order_.back();
    404 
    405     if (delegated_quad->shared_quad_state != delegated_shared_quad_state) {
    406       delegated_shared_quad_state = delegated_quad->shared_quad_state;
    407       output_shared_quad_state = render_pass->CreateAndAppendSharedQuadState();
    408       output_shared_quad_state->CopyFrom(delegated_shared_quad_state);
    409 
    410       if (is_root_delegated_render_pass) {
    411         gfx::Transform delegated_frame_to_target_transform = draw_transform();
    412         delegated_frame_to_target_transform.Scale(inverse_device_scale_factor_,
    413                                                   inverse_device_scale_factor_);
    414 
    415         output_shared_quad_state->content_to_target_transform.ConcatTransform(
    416             delegated_frame_to_target_transform);
    417 
    418         if (render_target() == this) {
    419           DCHECK(!is_clipped());
    420           DCHECK(render_surface());
    421           DCHECK_EQ(0, num_unclipped_descendants());
    422           output_shared_quad_state->clip_rect =
    423               MathUtil::MapEnclosingClippedRect(
    424                   delegated_frame_to_target_transform,
    425                   output_shared_quad_state->clip_rect);
    426         } else {
    427           gfx::Rect clip_rect = drawable_content_rect();
    428           if (output_shared_quad_state->is_clipped) {
    429             clip_rect.Intersect(MathUtil::MapEnclosingClippedRect(
    430                 delegated_frame_to_target_transform,
    431                 output_shared_quad_state->clip_rect));
    432           }
    433           output_shared_quad_state->clip_rect = clip_rect;
    434           output_shared_quad_state->is_clipped = true;
    435         }
    436 
    437         output_shared_quad_state->opacity *= draw_opacity();
    438       }
    439     }
    440     DCHECK(output_shared_quad_state);
    441 
    442     gfx::Transform quad_content_to_delegated_target_space =
    443         output_shared_quad_state->content_to_target_transform;
    444     if (!is_root_delegated_render_pass) {
    445       quad_content_to_delegated_target_space.ConcatTransform(
    446           delegated_render_pass->transform_to_root_target);
    447       quad_content_to_delegated_target_space.ConcatTransform(draw_transform());
    448     }
    449 
    450     gfx::Rect quad_visible_rect =
    451         occlusion_tracker.GetCurrentOcclusionForLayer(
    452                               quad_content_to_delegated_target_space)
    453             .GetUnoccludedContentRect(delegated_quad->visible_rect);
    454 
    455     if (quad_visible_rect.IsEmpty())
    456       continue;
    457 
    458     if (delegated_quad->material != DrawQuad::RENDER_PASS) {
    459       DrawQuad* output_quad = render_pass->CopyFromAndAppendDrawQuad(
    460           delegated_quad, output_shared_quad_state);
    461       output_quad->visible_rect = quad_visible_rect;
    462     } else {
    463       RenderPassId delegated_contributing_render_pass_id =
    464           RenderPassDrawQuad::MaterialCast(delegated_quad)->render_pass_id;
    465       RenderPassId output_contributing_render_pass_id(-1, -1);
    466 
    467       bool present =
    468           ConvertDelegatedRenderPassId(delegated_contributing_render_pass_id,
    469                                        &output_contributing_render_pass_id);
    470 
    471       // The frame may have a RenderPassDrawQuad that points to a RenderPass not
    472       // part of the frame. Just ignore these quads.
    473       if (present) {
    474         DCHECK(output_contributing_render_pass_id !=
    475                append_quads_data->render_pass_id);
    476 
    477         RenderPassDrawQuad* output_quad =
    478             render_pass->CopyFromAndAppendRenderPassDrawQuad(
    479                 RenderPassDrawQuad::MaterialCast(delegated_quad),
    480                 output_shared_quad_state,
    481                 output_contributing_render_pass_id);
    482         output_quad->visible_rect = quad_visible_rect;
    483       }
    484     }
    485   }
    486 }
    487 
    488 const char* DelegatedRendererLayerImpl::LayerTypeAsString() const {
    489   return "cc::DelegatedRendererLayerImpl";
    490 }
    491 
    492 void DelegatedRendererLayerImpl::ClearChildId() {
    493   if (!child_id_)
    494     return;
    495 
    496   if (own_child_id_) {
    497     ResourceProvider* provider = layer_tree_impl()->resource_provider();
    498     provider->DestroyChild(child_id_);
    499   }
    500 
    501   resources_.clear();
    502   child_id_ = 0;
    503 }
    504 
    505 }  // namespace cc
    506