| OLD | NEW | 
| (Empty) |  | 
 |    1 // Copyright 2013 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/resources/tile_bundle.h" | 
 |    6  | 
 |    7 #include <algorithm> | 
 |    8  | 
 |    9 #include "cc/resources/picture_layer_tiling.h" | 
 |   10 #include "cc/resources/tile.h" | 
 |   11 #include "cc/resources/tile_manager.h" | 
 |   12  | 
 |   13 namespace cc { | 
 |   14  | 
 |   15 TileBundle::Id TileBundle::s_next_id_ = 0; | 
 |   16  | 
 |   17 TileBundle::TileBundle(TileManager* tile_manager, | 
 |   18                        int offset_x, | 
 |   19                        int offset_y, | 
 |   20                        int width, | 
 |   21                        int height) | 
 |   22     : RefCountedManaged<TileBundle>(tile_manager), | 
 |   23       tile_manager_(tile_manager), | 
 |   24       index_offset_x_(offset_x), | 
 |   25       index_offset_y_(offset_y), | 
 |   26       index_count_x_(width), | 
 |   27       index_count_y_(height), | 
 |   28       conservative_tile_count_(0), | 
 |   29       needs_tile_swap_(false), | 
 |   30       id_(s_next_id_++) { | 
 |   31   tiles_[ACTIVE_TREE].resize(index_count_y_); | 
 |   32   for (int i = 0; i < index_count_y_; ++i) | 
 |   33     tiles_[ACTIVE_TREE][i].resize(index_count_x_); | 
 |   34  | 
 |   35   tiles_[PENDING_TREE].resize(index_count_y_); | 
 |   36   for (int i = 0; i < index_count_y_; ++i) | 
 |   37     tiles_[PENDING_TREE][i].resize(index_count_x_); | 
 |   38 } | 
 |   39  | 
 |   40 TileBundle::~TileBundle() {} | 
 |   41  | 
 |   42 Tile* TileBundle::TileAt(WhichTree tree, int index_x, int index_y) { | 
 |   43   DCHECK(!needs_tile_swap_); | 
 |   44  | 
 |   45   UpdateToLocalIndex(&index_x, &index_y); | 
 |   46   return TileAtLocalIndex(tree, index_x, index_y); | 
 |   47 } | 
 |   48  | 
 |   49 Tile* TileBundle::TileAtLocalIndex(WhichTree tree, int index_x, int index_y) { | 
 |   50   return tiles_[tree][index_y][index_x].get(); | 
 |   51 } | 
 |   52  | 
 |   53 void TileBundle::SetPriority(WhichTree tree, const TilePriority& priority) { | 
 |   54   DCHECK(!needs_tile_swap_); | 
 |   55  | 
 |   56   if (priority_[tree] == priority) | 
 |   57     return; | 
 |   58  | 
 |   59   priority_[tree] = priority; | 
 |   60   tile_manager_->DidChangeTileBundlePriority(this); | 
 |   61 } | 
 |   62  | 
 |   63 TilePriority TileBundle::GetPriority(WhichTree tree) const { | 
 |   64   return priority_[tree]; | 
 |   65 } | 
 |   66  | 
 |   67 bool TileBundle::RemoveTileAt(WhichTree tree, int index_x, int index_y) { | 
 |   68   DCHECK(!needs_tile_swap_); | 
 |   69  | 
 |   70   UpdateToLocalIndex(&index_x, &index_y); | 
 |   71   bool removed = !!tiles_[tree][index_y][index_x]; | 
 |   72   tiles_[tree][index_y][index_x] = NULL; | 
 |   73   if (removed) | 
 |   74     --conservative_tile_count_; | 
 |   75   return removed; | 
 |   76 } | 
 |   77  | 
 |   78 void TileBundle::AddTileAt(WhichTree tree, | 
 |   79                            int index_x, | 
 |   80                            int index_y, | 
 |   81                            scoped_refptr<Tile> tile) { | 
 |   82   DCHECK(!needs_tile_swap_); | 
 |   83  | 
 |   84   UpdateToLocalIndex(&index_x, &index_y); | 
 |   85   DCHECK(!tiles_[tree][index_y][index_x]); | 
 |   86   tiles_[tree][index_y][index_x] = tile; | 
 |   87   ++conservative_tile_count_; | 
 |   88 } | 
 |   89  | 
 |   90 void TileBundle::DidBecomeRecycled() { | 
 |   91   priority_[ACTIVE_TREE] = TilePriority(); | 
 |   92   needs_tile_swap_ = !needs_tile_swap_; | 
 |   93 } | 
 |   94  | 
 |   95 void TileBundle::DidBecomeActive() { | 
 |   96   priority_[ACTIVE_TREE] = priority_[PENDING_TREE]; | 
 |   97   priority_[PENDING_TREE] = TilePriority(); | 
 |   98   tiles_[ACTIVE_TREE].swap(tiles_[PENDING_TREE]); | 
 |   99   needs_tile_swap_ = false; | 
 |  100 } | 
 |  101  | 
 |  102 void TileBundle::UpdateToLocalIndex(int* index_x, int* index_y) { | 
 |  103   DCHECK(index_x); | 
 |  104   DCHECK(index_y); | 
 |  105  | 
 |  106   *index_x -= index_offset_x_; | 
 |  107   *index_y -= index_offset_y_; | 
 |  108  | 
 |  109   DCHECK_GE(*index_x, 0); | 
 |  110   DCHECK_GE(*index_y, 0); | 
 |  111   DCHECK_LT(*index_x, index_count_x_); | 
 |  112   DCHECK_LT(*index_x, index_count_y_); | 
 |  113 } | 
 |  114  | 
 |  115 void TileBundle::SwapTilesIfRequired() { | 
 |  116   if (!needs_tile_swap_) | 
 |  117     return; | 
 |  118  | 
 |  119   std::swap(priority_[ACTIVE_TREE], priority_[PENDING_TREE]); | 
 |  120   tiles_[ACTIVE_TREE].swap(tiles_[PENDING_TREE]); | 
 |  121   needs_tile_swap_ = false; | 
 |  122 } | 
 |  123  | 
 |  124 TileBundle::Iterator::Iterator(TileBundle* bundle) | 
 |  125     : bundle_(bundle), | 
 |  126       current_tile_(NULL), | 
 |  127       index_x_(0), | 
 |  128       index_y_(0), | 
 |  129       current_tree_(ACTIVE_TREE), | 
 |  130       min_tree_(ACTIVE_TREE), | 
 |  131       max_tree_(PENDING_TREE) { | 
 |  132   if (!InitializeNewTile()) | 
 |  133     ++(*this); | 
 |  134 } | 
 |  135  | 
 |  136 TileBundle::Iterator::Iterator(TileBundle* bundle, WhichTree tree) | 
 |  137     : bundle_(bundle), | 
 |  138       current_tile_(NULL), | 
 |  139       index_x_(0), | 
 |  140       index_y_(0), | 
 |  141       current_tree_(tree), | 
 |  142       min_tree_(tree), | 
 |  143       max_tree_(tree) { | 
 |  144   if (!InitializeNewTile()) | 
 |  145     ++(*this); | 
 |  146 } | 
 |  147  | 
 |  148 TileBundle::Iterator::~Iterator() {} | 
 |  149  | 
 |  150 TileBundle::Iterator& TileBundle::Iterator::operator++() { | 
 |  151   do { | 
 |  152     current_tree_ = static_cast<WhichTree>(current_tree_ + 1); | 
 |  153     if (current_tree_ > max_tree_) { | 
 |  154       current_tree_ = min_tree_; | 
 |  155       ++index_x_; | 
 |  156       if (index_x_ >= bundle_->index_count_x_) { | 
 |  157         index_x_ = 0; | 
 |  158         ++index_y_; | 
 |  159         if (index_y_ >= bundle_->index_count_y_) | 
 |  160           break; | 
 |  161       } | 
 |  162     } | 
 |  163   } while (!InitializeNewTile()); | 
 |  164  | 
 |  165   return *this; | 
 |  166 } | 
 |  167  | 
 |  168 bool TileBundle::Iterator::InitializeNewTile() { | 
 |  169   Tile* tile = bundle_->TileAtLocalIndex(current_tree_, index_x_, index_y_); | 
 |  170  | 
 |  171   // We succeed only if we have a tile and it is different | 
 |  172   // from what we currently have. This is to avoid the iterator | 
 |  173   // returning the same tile twice when it's shared between trees. | 
 |  174   if (!tile || tile == current_tile_) | 
 |  175     return false; | 
 |  176  | 
 |  177   current_tile_ = tile; | 
 |  178   return true; | 
 |  179 } | 
 |  180  | 
 |  181 }  // namespace cc | 
| OLD | NEW |