Home | History | Annotate | Download | only in trees
      1 // Copyright 2011 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/trees/layer_tree_impl.h"
      6 
      7 #include "base/debug/trace_event.h"
      8 #include "cc/animation/keyframed_animation_curve.h"
      9 #include "cc/animation/scrollbar_animation_controller.h"
     10 #include "cc/base/math_util.h"
     11 #include "cc/base/util.h"
     12 #include "cc/debug/traced_value.h"
     13 #include "cc/layers/heads_up_display_layer_impl.h"
     14 #include "cc/layers/layer.h"
     15 #include "cc/layers/render_surface_impl.h"
     16 #include "cc/layers/scrollbar_layer_impl_base.h"
     17 #include "cc/resources/ui_resource_request.h"
     18 #include "cc/trees/layer_tree_host_common.h"
     19 #include "cc/trees/layer_tree_host_impl.h"
     20 #include "ui/gfx/size_conversions.h"
     21 #include "ui/gfx/vector2d_conversions.h"
     22 
     23 namespace cc {
     24 
     25 LayerTreeImpl::LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl)
     26     : layer_tree_host_impl_(layer_tree_host_impl),
     27       source_frame_number_(-1),
     28       hud_layer_(0),
     29       root_scroll_layer_(NULL),
     30       currently_scrolling_layer_(NULL),
     31       root_layer_scroll_offset_delegate_(NULL),
     32       background_color_(0),
     33       has_transparent_background_(false),
     34       page_scale_layer_(NULL),
     35       inner_viewport_scroll_layer_(NULL),
     36       outer_viewport_scroll_layer_(NULL),
     37       page_scale_factor_(1),
     38       page_scale_delta_(1),
     39       sent_page_scale_delta_(1),
     40       min_page_scale_factor_(0),
     41       max_page_scale_factor_(0),
     42       scrolling_layer_id_from_previous_tree_(0),
     43       contents_textures_purged_(false),
     44       viewport_size_invalid_(false),
     45       needs_update_draw_properties_(true),
     46       needs_full_tree_sync_(true),
     47       next_activation_forces_redraw_(false) {
     48 }
     49 
     50 LayerTreeImpl::~LayerTreeImpl() {
     51   // Need to explicitly clear the tree prior to destroying this so that
     52   // the LayerTreeImpl pointer is still valid in the LayerImpl dtor.
     53   root_layer_.reset();
     54 }
     55 
     56 static LayerImpl* FindRootScrollLayerRecursive(LayerImpl* layer) {
     57   if (!layer)
     58     return NULL;
     59 
     60   if (layer->scrollable())
     61     return layer;
     62 
     63   for (size_t i = 0; i < layer->children().size(); ++i) {
     64     LayerImpl* found = FindRootScrollLayerRecursive(layer->children()[i]);
     65     if (found)
     66       return found;
     67   }
     68 
     69   return NULL;
     70 }
     71 
     72 void LayerTreeImpl::SetRootLayer(scoped_ptr<LayerImpl> layer) {
     73   if (root_scroll_layer_)
     74     root_scroll_layer_->SetScrollOffsetDelegate(NULL);
     75   root_layer_ = layer.Pass();
     76   currently_scrolling_layer_ = NULL;
     77   root_scroll_layer_ = NULL;
     78 
     79   layer_tree_host_impl_->OnCanDrawStateChangedForTree();
     80 }
     81 
     82 void LayerTreeImpl::FindRootScrollLayer() {
     83   root_scroll_layer_ = FindRootScrollLayerRecursive(root_layer_.get());
     84 
     85   if (root_scroll_layer_) {
     86     UpdateMaxScrollOffset();
     87     root_scroll_layer_->SetScrollOffsetDelegate(
     88         root_layer_scroll_offset_delegate_);
     89   }
     90 
     91   if (scrolling_layer_id_from_previous_tree_) {
     92     currently_scrolling_layer_ = LayerTreeHostCommon::FindLayerInSubtree(
     93         root_layer_.get(),
     94         scrolling_layer_id_from_previous_tree_);
     95   }
     96 
     97   scrolling_layer_id_from_previous_tree_ = 0;
     98 }
     99 
    100 scoped_ptr<LayerImpl> LayerTreeImpl::DetachLayerTree() {
    101   // Clear all data structures that have direct references to the layer tree.
    102   scrolling_layer_id_from_previous_tree_ =
    103     currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0;
    104   if (root_scroll_layer_)
    105     root_scroll_layer_->SetScrollOffsetDelegate(NULL);
    106   root_scroll_layer_ = NULL;
    107   currently_scrolling_layer_ = NULL;
    108 
    109   render_surface_layer_list_.clear();
    110   set_needs_update_draw_properties();
    111   return root_layer_.Pass();
    112 }
    113 
    114 void LayerTreeImpl::PushPropertiesTo(LayerTreeImpl* target_tree) {
    115   // The request queue should have been processed and does not require a push.
    116   DCHECK_EQ(ui_resource_request_queue_.size(), 0u);
    117 
    118   if (next_activation_forces_redraw_) {
    119     layer_tree_host_impl_->SetFullRootLayerDamage();
    120     next_activation_forces_redraw_ = false;
    121   }
    122 
    123   target_tree->PassSwapPromises(&swap_promise_list_);
    124 
    125   target_tree->SetPageScaleFactorAndLimits(
    126       page_scale_factor(), min_page_scale_factor(), max_page_scale_factor());
    127   target_tree->SetPageScaleDelta(
    128       target_tree->page_scale_delta() / target_tree->sent_page_scale_delta());
    129   target_tree->set_sent_page_scale_delta(1);
    130 
    131   if (settings().use_pinch_virtual_viewport) {
    132     target_tree->SetViewportLayersFromIds(
    133         page_scale_layer_->id(),
    134         inner_viewport_scroll_layer_->id(),
    135         outer_viewport_scroll_layer_ ? outer_viewport_scroll_layer_->id()
    136                                      : Layer::INVALID_ID);
    137   }
    138   // This should match the property synchronization in
    139   // LayerTreeHost::finishCommitOnImplThread().
    140   target_tree->set_source_frame_number(source_frame_number());
    141   target_tree->set_background_color(background_color());
    142   target_tree->set_has_transparent_background(has_transparent_background());
    143 
    144   if (ContentsTexturesPurged())
    145     target_tree->SetContentsTexturesPurged();
    146   else
    147     target_tree->ResetContentsTexturesPurged();
    148 
    149   if (ViewportSizeInvalid())
    150     target_tree->SetViewportSizeInvalid();
    151   else
    152     target_tree->ResetViewportSizeInvalid();
    153 
    154   if (hud_layer())
    155     target_tree->set_hud_layer(static_cast<HeadsUpDisplayLayerImpl*>(
    156         LayerTreeHostCommon::FindLayerInSubtree(
    157             target_tree->root_layer(), hud_layer()->id())));
    158   else
    159     target_tree->set_hud_layer(NULL);
    160 }
    161 
    162 LayerImpl* LayerTreeImpl::RootScrollLayer() const {
    163   return root_scroll_layer_;
    164 }
    165 
    166 LayerImpl* LayerTreeImpl::RootContainerLayer() const {
    167   return root_scroll_layer_ ? root_scroll_layer_->parent() : NULL;
    168 }
    169 
    170 LayerImpl* LayerTreeImpl::CurrentlyScrollingLayer() const {
    171   DCHECK(IsActiveTree());
    172   return currently_scrolling_layer_;
    173 }
    174 
    175 void LayerTreeImpl::SetCurrentlyScrollingLayer(LayerImpl* layer) {
    176   if (currently_scrolling_layer_ == layer)
    177     return;
    178 
    179   if (currently_scrolling_layer_ &&
    180       currently_scrolling_layer_->scrollbar_animation_controller())
    181     currently_scrolling_layer_->scrollbar_animation_controller()->
    182         DidScrollGestureEnd(CurrentPhysicalTimeTicks());
    183   currently_scrolling_layer_ = layer;
    184   if (layer && layer->scrollbar_animation_controller())
    185     layer->scrollbar_animation_controller()->DidScrollGestureBegin();
    186 }
    187 
    188 void LayerTreeImpl::ClearCurrentlyScrollingLayer() {
    189   SetCurrentlyScrollingLayer(NULL);
    190   scrolling_layer_id_from_previous_tree_ = 0;
    191 }
    192 
    193 void LayerTreeImpl::SetPageScaleFactorAndLimits(float page_scale_factor,
    194     float min_page_scale_factor, float max_page_scale_factor) {
    195   if (!page_scale_factor)
    196     return;
    197 
    198   min_page_scale_factor_ = min_page_scale_factor;
    199   max_page_scale_factor_ = max_page_scale_factor;
    200   page_scale_factor_ = page_scale_factor;
    201 
    202   if (root_layer_scroll_offset_delegate_) {
    203     root_layer_scroll_offset_delegate_->SetTotalPageScaleFactor(
    204         total_page_scale_factor());
    205   }
    206 }
    207 
    208 void LayerTreeImpl::SetPageScaleDelta(float delta) {
    209   // Clamp to the current min/max limits.
    210   float total = page_scale_factor_ * delta;
    211   if (min_page_scale_factor_ && total < min_page_scale_factor_)
    212     delta = min_page_scale_factor_ / page_scale_factor_;
    213   else if (max_page_scale_factor_ && total > max_page_scale_factor_)
    214     delta = max_page_scale_factor_ / page_scale_factor_;
    215 
    216   if (delta == page_scale_delta_)
    217     return;
    218 
    219   page_scale_delta_ = delta;
    220 
    221   if (IsActiveTree()) {
    222     LayerTreeImpl* pending_tree = layer_tree_host_impl_->pending_tree();
    223     if (pending_tree) {
    224       DCHECK_EQ(1, pending_tree->sent_page_scale_delta());
    225       pending_tree->SetPageScaleDelta(
    226           page_scale_delta_ / sent_page_scale_delta_);
    227     }
    228   }
    229 
    230   UpdateMaxScrollOffset();
    231   set_needs_update_draw_properties();
    232 
    233   if (root_layer_scroll_offset_delegate_) {
    234     root_layer_scroll_offset_delegate_->SetTotalPageScaleFactor(
    235         total_page_scale_factor());
    236   }
    237 }
    238 
    239 gfx::SizeF LayerTreeImpl::ScrollableViewportSize() const {
    240   return gfx::ScaleSize(layer_tree_host_impl_->UnscaledScrollableViewportSize(),
    241                         1.0f / total_page_scale_factor());
    242 }
    243 
    244 gfx::Rect LayerTreeImpl::RootScrollLayerDeviceViewportBounds() const {
    245   if (!root_scroll_layer_ || root_scroll_layer_->children().empty())
    246     return gfx::Rect();
    247   LayerImpl* layer = root_scroll_layer_->children()[0];
    248   return MathUtil::MapClippedRect(
    249       layer->screen_space_transform(),
    250       gfx::Rect(layer->content_bounds()));
    251 }
    252 
    253 void LayerTreeImpl::UpdateMaxScrollOffset() {
    254   LayerImpl* root_scroll = RootScrollLayer();
    255   if (!root_scroll || !root_scroll->children().size())
    256     return;
    257 
    258   gfx::Vector2dF max_scroll = gfx::Rect(ScrollableSize()).bottom_right() -
    259       gfx::RectF(ScrollableViewportSize()).bottom_right();
    260 
    261   // The viewport may be larger than the contents in some cases, such as
    262   // having a vertical scrollbar but no horizontal overflow.
    263   max_scroll.SetToMax(gfx::Vector2dF());
    264 
    265   root_scroll_layer_->SetMaxScrollOffset(gfx::ToFlooredVector2d(max_scroll));
    266 }
    267 
    268 static void ApplySentScrollDeltasFromAbortedCommitTo(LayerImpl* layer) {
    269   layer->ApplySentScrollDeltasFromAbortedCommit();
    270 }
    271 
    272 void LayerTreeImpl::ApplySentScrollAndScaleDeltasFromAbortedCommit() {
    273   DCHECK(IsActiveTree());
    274 
    275   page_scale_factor_ *= sent_page_scale_delta_;
    276   page_scale_delta_ /= sent_page_scale_delta_;
    277   sent_page_scale_delta_ = 1.f;
    278 
    279   if (!root_layer())
    280     return;
    281 
    282   LayerTreeHostCommon::CallFunctionForSubtree(
    283       root_layer(), base::Bind(&ApplySentScrollDeltasFromAbortedCommitTo));
    284 }
    285 
    286 static void ApplyScrollDeltasSinceBeginMainFrameTo(LayerImpl* layer) {
    287   layer->ApplyScrollDeltasSinceBeginMainFrame();
    288 }
    289 
    290 void LayerTreeImpl::ApplyScrollDeltasSinceBeginMainFrame() {
    291   DCHECK(IsPendingTree());
    292   if (!root_layer())
    293     return;
    294 
    295   LayerTreeHostCommon::CallFunctionForSubtree(
    296       root_layer(), base::Bind(&ApplyScrollDeltasSinceBeginMainFrameTo));
    297 }
    298 
    299 void LayerTreeImpl::SetViewportLayersFromIds(
    300     int page_scale_layer_id,
    301     int inner_viewport_scroll_layer_id,
    302     int outer_viewport_scroll_layer_id) {
    303   page_scale_layer_ = LayerById(page_scale_layer_id);
    304   DCHECK(page_scale_layer_);
    305 
    306   inner_viewport_scroll_layer_ =
    307       LayerById(inner_viewport_scroll_layer_id);
    308   DCHECK(inner_viewport_scroll_layer_);
    309 
    310   outer_viewport_scroll_layer_ =
    311       LayerById(outer_viewport_scroll_layer_id);
    312   DCHECK(outer_viewport_scroll_layer_ ||
    313          outer_viewport_scroll_layer_id == Layer::INVALID_ID);
    314 }
    315 
    316 void LayerTreeImpl::ClearViewportLayers() {
    317   page_scale_layer_ = NULL;
    318   inner_viewport_scroll_layer_ = NULL;
    319   outer_viewport_scroll_layer_ = NULL;
    320 }
    321 
    322 // TODO(wjmaclean) This needs to go away, and be replaced with a single core
    323 // of login that works for both scrollbar layer types. This is already planned
    324 // as part of the larger pinch-zoom re-factoring viewport.
    325 void LayerTreeImpl::UpdateSolidColorScrollbars() {
    326   LayerImpl* root_scroll = RootScrollLayer();
    327   DCHECK(root_scroll);
    328   DCHECK(IsActiveTree());
    329 
    330   gfx::RectF scrollable_viewport(
    331       gfx::PointAtOffsetFromOrigin(root_scroll->TotalScrollOffset()),
    332       ScrollableViewportSize());
    333   float vertical_adjust = 0.0f;
    334   if (RootContainerLayer())
    335     vertical_adjust =
    336         layer_tree_host_impl_->UnscaledScrollableViewportSize().height() -
    337         RootContainerLayer()->bounds().height();
    338   if (ScrollbarLayerImplBase* horiz =
    339           root_scroll->horizontal_scrollbar_layer()) {
    340     horiz->SetVerticalAdjust(vertical_adjust);
    341     horiz->SetVisibleToTotalLengthRatio(
    342         scrollable_viewport.width() / ScrollableSize().width());
    343   }
    344   if (ScrollbarLayerImplBase* vertical =
    345           root_scroll->vertical_scrollbar_layer()) {
    346     vertical->SetVerticalAdjust(vertical_adjust);
    347     vertical->SetVisibleToTotalLengthRatio(
    348         scrollable_viewport.height() / ScrollableSize().height());
    349   }
    350 }
    351 
    352 void LayerTreeImpl::UpdateDrawProperties() {
    353   if (IsActiveTree() && RootScrollLayer() && RootContainerLayer())
    354     UpdateRootScrollLayerSizeDelta();
    355 
    356   if (IsActiveTree() &&
    357       RootContainerLayer()
    358       && !RootContainerLayer()->masks_to_bounds()) {
    359     UpdateSolidColorScrollbars();
    360   }
    361 
    362   needs_update_draw_properties_ = false;
    363   render_surface_layer_list_.clear();
    364 
    365   // For max_texture_size.
    366   if (!layer_tree_host_impl_->renderer())
    367     return;
    368 
    369   if (!root_layer())
    370     return;
    371 
    372   {
    373     TRACE_EVENT2("cc",
    374                  "LayerTreeImpl::UpdateDrawProperties",
    375                  "IsActive",
    376                  IsActiveTree(),
    377                  "SourceFrameNumber",
    378                  source_frame_number_);
    379     LayerImpl* page_scale_layer =
    380         page_scale_layer_ ? page_scale_layer_ : RootContainerLayer();
    381     bool can_render_to_separate_surface =
    382         !output_surface()->ForcedDrawToSoftwareDevice();
    383     LayerTreeHostCommon::CalcDrawPropsImplInputs inputs(
    384         root_layer(),
    385         DrawViewportSize(),
    386         layer_tree_host_impl_->DrawTransform(),
    387         device_scale_factor(),
    388         total_page_scale_factor(),
    389         page_scale_layer,
    390         MaxTextureSize(),
    391         settings().can_use_lcd_text,
    392         can_render_to_separate_surface,
    393         settings().layer_transforms_should_scale_layer_contents,
    394         &render_surface_layer_list_);
    395     LayerTreeHostCommon::CalculateDrawProperties(&inputs);
    396   }
    397 
    398   {
    399     TRACE_EVENT2("cc",
    400                  "LayerTreeImpl::UpdateTilePriorities",
    401                  "IsActive",
    402                  IsActiveTree(),
    403                  "SourceFrameNumber",
    404                  source_frame_number_);
    405     // LayerIterator is used here instead of CallFunctionForSubtree to only
    406     // UpdateTilePriorities on layers that will be visible (and thus have valid
    407     // draw properties) and not because any ordering is required.
    408     typedef LayerIterator<LayerImpl,
    409                           LayerImplList,
    410                           RenderSurfaceImpl,
    411                           LayerIteratorActions::FrontToBack> LayerIteratorType;
    412     LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
    413     for (LayerIteratorType it =
    414              LayerIteratorType::Begin(&render_surface_layer_list_);
    415          it != end;
    416          ++it) {
    417       if (!it.represents_itself())
    418         continue;
    419       LayerImpl* layer = *it;
    420 
    421       layer->UpdateTilePriorities();
    422       if (layer->mask_layer())
    423         layer->mask_layer()->UpdateTilePriorities();
    424       if (layer->replica_layer() && layer->replica_layer()->mask_layer())
    425         layer->replica_layer()->mask_layer()->UpdateTilePriorities();
    426     }
    427   }
    428 
    429   DCHECK(!needs_update_draw_properties_) <<
    430       "CalcDrawProperties should not set_needs_update_draw_properties()";
    431 }
    432 
    433 const LayerImplList& LayerTreeImpl::RenderSurfaceLayerList() const {
    434   // If this assert triggers, then the list is dirty.
    435   DCHECK(!needs_update_draw_properties_);
    436   return render_surface_layer_list_;
    437 }
    438 
    439 gfx::Size LayerTreeImpl::ScrollableSize() const {
    440   if (!root_scroll_layer_ || root_scroll_layer_->children().empty())
    441     return gfx::Size();
    442   return root_scroll_layer_->children()[0]->bounds();
    443 }
    444 
    445 LayerImpl* LayerTreeImpl::LayerById(int id) {
    446   LayerIdMap::iterator iter = layer_id_map_.find(id);
    447   return iter != layer_id_map_.end() ? iter->second : NULL;
    448 }
    449 
    450 void LayerTreeImpl::RegisterLayer(LayerImpl* layer) {
    451   DCHECK(!LayerById(layer->id()));
    452   layer_id_map_[layer->id()] = layer;
    453 }
    454 
    455 void LayerTreeImpl::UnregisterLayer(LayerImpl* layer) {
    456   DCHECK(LayerById(layer->id()));
    457   layer_id_map_.erase(layer->id());
    458 }
    459 
    460 void LayerTreeImpl::PushPersistedState(LayerTreeImpl* pending_tree) {
    461   pending_tree->SetCurrentlyScrollingLayer(
    462       LayerTreeHostCommon::FindLayerInSubtree(pending_tree->root_layer(),
    463           currently_scrolling_layer_ ? currently_scrolling_layer_->id() : 0));
    464 }
    465 
    466 static void DidBecomeActiveRecursive(LayerImpl* layer) {
    467   layer->DidBecomeActive();
    468   for (size_t i = 0; i < layer->children().size(); ++i)
    469     DidBecomeActiveRecursive(layer->children()[i]);
    470 }
    471 
    472 void LayerTreeImpl::DidBecomeActive() {
    473   if (!root_layer())
    474     return;
    475 
    476   DidBecomeActiveRecursive(root_layer());
    477   FindRootScrollLayer();
    478 }
    479 
    480 bool LayerTreeImpl::ContentsTexturesPurged() const {
    481   return contents_textures_purged_;
    482 }
    483 
    484 void LayerTreeImpl::SetContentsTexturesPurged() {
    485   if (contents_textures_purged_)
    486     return;
    487   contents_textures_purged_ = true;
    488   layer_tree_host_impl_->OnCanDrawStateChangedForTree();
    489 }
    490 
    491 void LayerTreeImpl::ResetContentsTexturesPurged() {
    492   if (!contents_textures_purged_)
    493     return;
    494   contents_textures_purged_ = false;
    495   layer_tree_host_impl_->OnCanDrawStateChangedForTree();
    496 }
    497 
    498 bool LayerTreeImpl::ViewportSizeInvalid() const {
    499   return viewport_size_invalid_;
    500 }
    501 
    502 void LayerTreeImpl::SetViewportSizeInvalid() {
    503   viewport_size_invalid_ = true;
    504   layer_tree_host_impl_->OnCanDrawStateChangedForTree();
    505 }
    506 
    507 void LayerTreeImpl::ResetViewportSizeInvalid() {
    508   viewport_size_invalid_ = false;
    509   layer_tree_host_impl_->OnCanDrawStateChangedForTree();
    510 }
    511 
    512 Proxy* LayerTreeImpl::proxy() const {
    513   return layer_tree_host_impl_->proxy();
    514 }
    515 
    516 const LayerTreeSettings& LayerTreeImpl::settings() const {
    517   return layer_tree_host_impl_->settings();
    518 }
    519 
    520 const RendererCapabilitiesImpl& LayerTreeImpl::GetRendererCapabilities() const {
    521   return layer_tree_host_impl_->GetRendererCapabilities();
    522 }
    523 
    524 ContextProvider* LayerTreeImpl::context_provider() const {
    525   return output_surface()->context_provider();
    526 }
    527 
    528 OutputSurface* LayerTreeImpl::output_surface() const {
    529   return layer_tree_host_impl_->output_surface();
    530 }
    531 
    532 ResourceProvider* LayerTreeImpl::resource_provider() const {
    533   return layer_tree_host_impl_->resource_provider();
    534 }
    535 
    536 TileManager* LayerTreeImpl::tile_manager() const {
    537   return layer_tree_host_impl_->tile_manager();
    538 }
    539 
    540 FrameRateCounter* LayerTreeImpl::frame_rate_counter() const {
    541   return layer_tree_host_impl_->fps_counter();
    542 }
    543 
    544 PaintTimeCounter* LayerTreeImpl::paint_time_counter() const {
    545   return layer_tree_host_impl_->paint_time_counter();
    546 }
    547 
    548 MemoryHistory* LayerTreeImpl::memory_history() const {
    549   return layer_tree_host_impl_->memory_history();
    550 }
    551 
    552 bool LayerTreeImpl::device_viewport_valid_for_tile_management() const {
    553   return layer_tree_host_impl_->device_viewport_valid_for_tile_management();
    554 }
    555 
    556 bool LayerTreeImpl::IsActiveTree() const {
    557   return layer_tree_host_impl_->active_tree() == this;
    558 }
    559 
    560 bool LayerTreeImpl::IsPendingTree() const {
    561   return layer_tree_host_impl_->pending_tree() == this;
    562 }
    563 
    564 bool LayerTreeImpl::IsRecycleTree() const {
    565   return layer_tree_host_impl_->recycle_tree() == this;
    566 }
    567 
    568 LayerImpl* LayerTreeImpl::FindActiveTreeLayerById(int id) {
    569   LayerTreeImpl* tree = layer_tree_host_impl_->active_tree();
    570   if (!tree)
    571     return NULL;
    572   return tree->LayerById(id);
    573 }
    574 
    575 LayerImpl* LayerTreeImpl::FindPendingTreeLayerById(int id) {
    576   LayerTreeImpl* tree = layer_tree_host_impl_->pending_tree();
    577   if (!tree)
    578     return NULL;
    579   return tree->LayerById(id);
    580 }
    581 
    582 int LayerTreeImpl::MaxTextureSize() const {
    583   return layer_tree_host_impl_->GetRendererCapabilities().max_texture_size;
    584 }
    585 
    586 bool LayerTreeImpl::PinchGestureActive() const {
    587   return layer_tree_host_impl_->pinch_gesture_active();
    588 }
    589 
    590 base::TimeTicks LayerTreeImpl::CurrentFrameTimeTicks() const {
    591   return layer_tree_host_impl_->CurrentFrameTimeTicks();
    592 }
    593 
    594 base::Time LayerTreeImpl::CurrentFrameTime() const {
    595   return layer_tree_host_impl_->CurrentFrameTime();
    596 }
    597 
    598 base::TimeTicks LayerTreeImpl::CurrentPhysicalTimeTicks() const {
    599   return layer_tree_host_impl_->CurrentPhysicalTimeTicks();
    600 }
    601 
    602 void LayerTreeImpl::SetNeedsCommit() {
    603   layer_tree_host_impl_->SetNeedsCommit();
    604 }
    605 
    606 gfx::Size LayerTreeImpl::DrawViewportSize() const {
    607   return layer_tree_host_impl_->DrawViewportSize();
    608 }
    609 
    610 void LayerTreeImpl::StartScrollbarAnimation() {
    611   layer_tree_host_impl_->StartScrollbarAnimation();
    612 }
    613 
    614 void LayerTreeImpl::DidAnimateScrollOffset() {
    615   layer_tree_host_impl_->DidAnimateScrollOffset();
    616 }
    617 
    618 void LayerTreeImpl::SetNeedsRedraw() {
    619   layer_tree_host_impl_->SetNeedsRedraw();
    620 }
    621 
    622 const LayerTreeDebugState& LayerTreeImpl::debug_state() const {
    623   return layer_tree_host_impl_->debug_state();
    624 }
    625 
    626 float LayerTreeImpl::device_scale_factor() const {
    627   return layer_tree_host_impl_->device_scale_factor();
    628 }
    629 
    630 DebugRectHistory* LayerTreeImpl::debug_rect_history() const {
    631   return layer_tree_host_impl_->debug_rect_history();
    632 }
    633 
    634 AnimationRegistrar* LayerTreeImpl::animationRegistrar() const {
    635   return layer_tree_host_impl_->animation_registrar();
    636 }
    637 
    638 scoped_ptr<base::Value> LayerTreeImpl::AsValue() const {
    639   scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
    640   TracedValue::MakeDictIntoImplicitSnapshot(
    641       state.get(), "cc::LayerTreeImpl", this);
    642 
    643   state->Set("root_layer", root_layer_->AsValue().release());
    644 
    645   scoped_ptr<base::ListValue> render_surface_layer_list(new base::ListValue());
    646   typedef LayerIterator<LayerImpl,
    647                         LayerImplList,
    648                         RenderSurfaceImpl,
    649                         LayerIteratorActions::FrontToBack> LayerIteratorType;
    650   LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list_);
    651   for (LayerIteratorType it = LayerIteratorType::Begin(
    652            &render_surface_layer_list_); it != end; ++it) {
    653     if (!it.represents_itself())
    654       continue;
    655     render_surface_layer_list->Append(TracedValue::CreateIDRef(*it).release());
    656   }
    657 
    658   state->Set("render_surface_layer_list",
    659              render_surface_layer_list.release());
    660   return state.PassAs<base::Value>();
    661 }
    662 
    663 void LayerTreeImpl::SetRootLayerScrollOffsetDelegate(
    664     LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate) {
    665   if (root_layer_scroll_offset_delegate_ == root_layer_scroll_offset_delegate)
    666     return;
    667 
    668   root_layer_scroll_offset_delegate_ = root_layer_scroll_offset_delegate;
    669 
    670   if (root_scroll_layer_) {
    671     root_scroll_layer_->SetScrollOffsetDelegate(
    672         root_layer_scroll_offset_delegate_);
    673   }
    674 
    675   if (root_layer_scroll_offset_delegate_) {
    676     root_layer_scroll_offset_delegate_->SetScrollableSize(ScrollableSize());
    677     root_layer_scroll_offset_delegate_->SetTotalPageScaleFactor(
    678         total_page_scale_factor());
    679   }
    680 }
    681 
    682 void LayerTreeImpl::UpdateRootScrollLayerSizeDelta() {
    683   LayerImpl* root_scroll = RootScrollLayer();
    684   LayerImpl* root_container = RootContainerLayer();
    685   DCHECK(root_scroll);
    686   DCHECK(root_container);
    687   DCHECK(IsActiveTree());
    688 
    689   gfx::Vector2dF scrollable_viewport_size =
    690       gfx::RectF(ScrollableViewportSize()).bottom_right() - gfx::PointF();
    691 
    692   gfx::Vector2dF original_viewport_size =
    693       gfx::RectF(root_container->bounds()).bottom_right() -
    694       gfx::PointF();
    695   original_viewport_size.Scale(1 / page_scale_factor());
    696 
    697   root_scroll->SetFixedContainerSizeDelta(
    698       scrollable_viewport_size - original_viewport_size);
    699 }
    700 
    701 void LayerTreeImpl::QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise) {
    702   DCHECK(swap_promise);
    703   if (swap_promise_list_.size() > kMaxQueuedSwapPromiseNumber)
    704     BreakSwapPromises(SwapPromise::SWAP_PROMISE_LIST_OVERFLOW);
    705   swap_promise_list_.push_back(swap_promise.Pass());
    706 }
    707 
    708 void LayerTreeImpl::PassSwapPromises(
    709     ScopedPtrVector<SwapPromise>* new_swap_promise) {
    710   swap_promise_list_.insert_and_take(swap_promise_list_.end(),
    711                                      *new_swap_promise);
    712   new_swap_promise->clear();
    713 }
    714 
    715 void LayerTreeImpl::FinishSwapPromises(CompositorFrameMetadata* metadata) {
    716   for (size_t i = 0; i < swap_promise_list_.size(); i++)
    717     swap_promise_list_[i]->DidSwap(metadata);
    718   swap_promise_list_.clear();
    719 }
    720 
    721 void LayerTreeImpl::BreakSwapPromises(SwapPromise::DidNotSwapReason reason) {
    722   for (size_t i = 0; i < swap_promise_list_.size(); i++)
    723     swap_promise_list_[i]->DidNotSwap(reason);
    724   swap_promise_list_.clear();
    725 }
    726 
    727 void LayerTreeImpl::DidModifyTilePriorities() {
    728   layer_tree_host_impl_->DidModifyTilePriorities();
    729 }
    730 
    731 void LayerTreeImpl::set_ui_resource_request_queue(
    732     const UIResourceRequestQueue& queue) {
    733   ui_resource_request_queue_ = queue;
    734 }
    735 
    736 ResourceProvider::ResourceId LayerTreeImpl::ResourceIdForUIResource(
    737     UIResourceId uid) const {
    738   return layer_tree_host_impl_->ResourceIdForUIResource(uid);
    739 }
    740 
    741 bool LayerTreeImpl::IsUIResourceOpaque(UIResourceId uid) const {
    742   return layer_tree_host_impl_->IsUIResourceOpaque(uid);
    743 }
    744 
    745 void LayerTreeImpl::ProcessUIResourceRequestQueue() {
    746   while (ui_resource_request_queue_.size() > 0) {
    747     UIResourceRequest req = ui_resource_request_queue_.front();
    748     ui_resource_request_queue_.pop_front();
    749 
    750     switch (req.GetType()) {
    751       case UIResourceRequest::UIResourceCreate:
    752         layer_tree_host_impl_->CreateUIResource(req.GetId(), req.GetBitmap());
    753         break;
    754       case UIResourceRequest::UIResourceDelete:
    755         layer_tree_host_impl_->DeleteUIResource(req.GetId());
    756         break;
    757       case UIResourceRequest::UIResourceInvalidRequest:
    758         NOTREACHED();
    759         break;
    760     }
    761   }
    762 
    763   // If all UI resource evictions were not recreated by processing this queue,
    764   // then another commit is required.
    765   if (layer_tree_host_impl_->EvictedUIResourcesExist())
    766     layer_tree_host_impl_->SetNeedsCommit();
    767 }
    768 
    769 void LayerTreeImpl::AddLayerWithCopyOutputRequest(LayerImpl* layer) {
    770   // Only the active tree needs to know about layers with copy requests, as
    771   // they are aborted if not serviced during draw.
    772   DCHECK(IsActiveTree());
    773 
    774   DCHECK(std::find(layers_with_copy_output_request_.begin(),
    775                    layers_with_copy_output_request_.end(),
    776                    layer) == layers_with_copy_output_request_.end());
    777   layers_with_copy_output_request_.push_back(layer);
    778 }
    779 
    780 void LayerTreeImpl::RemoveLayerWithCopyOutputRequest(LayerImpl* layer) {
    781   // Only the active tree needs to know about layers with copy requests, as
    782   // they are aborted if not serviced during draw.
    783   DCHECK(IsActiveTree());
    784 
    785   std::vector<LayerImpl*>::iterator it = std::find(
    786       layers_with_copy_output_request_.begin(),
    787       layers_with_copy_output_request_.end(),
    788       layer);
    789   DCHECK(it != layers_with_copy_output_request_.end());
    790   layers_with_copy_output_request_.erase(it);
    791 }
    792 
    793 const std::vector<LayerImpl*>& LayerTreeImpl::LayersWithCopyOutputRequest()
    794     const {
    795   // Only the active tree needs to know about layers with copy requests, as
    796   // they are aborted if not serviced during draw.
    797   DCHECK(IsActiveTree());
    798 
    799   return layers_with_copy_output_request_;
    800 }
    801 
    802 }  // namespace cc
    803