| 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 offset_x_(offset_x), |
| 25 offset_y_(offset_y), |
| 26 width_(width), |
| 27 height_(height), |
| 28 conservative_count_(0u), |
| 29 needs_tile_swap_(false), |
| 30 id_(s_next_id_++) { |
| 31 tiles_[ACTIVE_TREE].resize(height_); |
| 32 for (int i = 0; i < height_; ++i) |
| 33 tiles_[ACTIVE_TREE][i].resize(width_); |
| 34 |
| 35 tiles_[PENDING_TREE].resize(height_); |
| 36 for (int i = 0; i < height_; ++i) |
| 37 tiles_[PENDING_TREE][i].resize(width_); |
| 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_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_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 -= offset_x_; |
| 107 *index_y -= offset_y_; |
| 108 |
| 109 DCHECK_GE(*index_x, 0); |
| 110 DCHECK_GE(*index_y, 0); |
| 111 DCHECK_LT(*index_x, width_); |
| 112 DCHECK_LT(*index_x, height_); |
| 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 active_tree_only_tile_(false), |
| 131 pending_tree_only_tile_(false) { |
| 132 trees_to_iterate_.push_back(ACTIVE_TREE); |
| 133 trees_to_iterate_.push_back(PENDING_TREE); |
| 134 |
| 135 for (int i = 0; i < NUM_TREES; ++i) |
| 136 priority_[i] = NULL; |
| 137 |
| 138 if (!InitializeNewTileForTree(trees_to_iterate_[0])) |
| 139 ++(*this); |
| 140 } |
| 141 |
| 142 TileBundle::Iterator::Iterator(TileBundle* bundle, WhichTree tree) |
| 143 : bundle_(bundle), |
| 144 current_tile_(NULL), |
| 145 index_x_(0), |
| 146 index_y_(0), |
| 147 current_tree_(tree), |
| 148 active_tree_only_tile_(false), |
| 149 pending_tree_only_tile_(false) { |
| 150 trees_to_iterate_.push_back(tree); |
| 151 |
| 152 for (int i = 0; i < NUM_TREES; ++i) |
| 153 priority_[i] = NULL; |
| 154 |
| 155 if (!InitializeNewTileForTree(trees_to_iterate_[0])) |
| 156 ++(*this); |
| 157 } |
| 158 |
| 159 TileBundle::Iterator::~Iterator() {} |
| 160 |
| 161 TileBundle::Iterator& TileBundle::Iterator::operator++() { |
| 162 DCHECK_GT(trees_to_iterate_.size(), 0u); |
| 163 DCHECK_LE(trees_to_iterate_.size(), 2u); |
| 164 |
| 165 if (current_tree_ == trees_to_iterate_[0] && |
| 166 trees_to_iterate_.size() > 1 && |
| 167 InitializeNewTileForTree(trees_to_iterate_[1])) |
| 168 return *this; |
| 169 |
| 170 do { |
| 171 ++index_x_; |
| 172 if (index_x_ >= bundle_->width_) { |
| 173 index_x_ = 0; |
| 174 ++index_y_; |
| 175 } |
| 176 } while (index_y_ < bundle_->height_ && |
| 177 !InitializeNewTileForTree(trees_to_iterate_[0]) && |
| 178 (trees_to_iterate_.size() == 1 || |
| 179 !InitializeNewTileForTree(trees_to_iterate_[1]))); |
| 180 return *this; |
| 181 } |
| 182 |
| 183 bool TileBundle::Iterator::InitializeNewTileForTree(WhichTree tree) { |
| 184 Tile* tile = bundle_->TileAtLocalIndex(tree, index_x_, index_y_); |
| 185 if (!tile || tile == current_tile_) |
| 186 return false; |
| 187 |
| 188 current_tree_ = tree; |
| 189 current_tile_ = tile; |
| 190 priority_[tree] = &bundle_->priority_[tree]; |
| 191 |
| 192 WhichTree twin_tree = (tree == ACTIVE_TREE) ? PENDING_TREE : ACTIVE_TREE; |
| 193 Tile* twin_tile = bundle_->TileAtLocalIndex(twin_tree, index_x_, index_y_); |
| 194 if (twin_tile == tile) { |
| 195 priority_[twin_tree] = &bundle_->priority_[twin_tree]; |
| 196 active_tree_only_tile_ = false; |
| 197 pending_tree_only_tile_ = false; |
| 198 } else { |
| 199 active_tree_only_tile_ = (tree == ACTIVE_TREE); |
| 200 pending_tree_only_tile_ = (tree == PENDING_TREE); |
| 201 } |
| 202 return true; |
| 203 } |
| 204 |
| 205 } // namespace cc |
| OLD | NEW |