Chromium Code Reviews| Index: cc/resources/tile_bundle.cc | 
| diff --git a/cc/resources/tile_bundle.cc b/cc/resources/tile_bundle.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..95bc31af6363f7c681b62a9be50dd0a7f7a84636 | 
| --- /dev/null | 
| +++ b/cc/resources/tile_bundle.cc | 
| @@ -0,0 +1,181 @@ | 
| +// Copyright 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "cc/resources/tile_bundle.h" | 
| + | 
| +#include <algorithm> | 
| + | 
| +#include "cc/resources/picture_layer_tiling.h" | 
| +#include "cc/resources/tile.h" | 
| +#include "cc/resources/tile_manager.h" | 
| + | 
| +namespace cc { | 
| + | 
| +TileBundle::Id TileBundle::s_next_id_ = 0; | 
| + | 
| +TileBundle::TileBundle(TileManager* tile_manager, | 
| + int offset_x, | 
| + int offset_y, | 
| + int width, | 
| + int height) | 
| + : RefCountedManaged<TileBundle>(tile_manager), | 
| + tile_manager_(tile_manager), | 
| + offset_x_(offset_x), | 
| + offset_y_(offset_y), | 
| + width_(width), | 
| + height_(height), | 
| + conservative_count_(0u), | 
| + needs_tile_swap_(false), | 
| + id_(s_next_id_++) { | 
| + tiles_[ACTIVE_TREE].resize(height_); | 
| + for (int i = 0; i < height_; ++i) | 
| + tiles_[ACTIVE_TREE][i].resize(width_); | 
| + | 
| + tiles_[PENDING_TREE].resize(height_); | 
| + for (int i = 0; i < height_; ++i) | 
| + tiles_[PENDING_TREE][i].resize(width_); | 
| +} | 
| + | 
| +TileBundle::~TileBundle() {} | 
| + | 
| +Tile* TileBundle::TileAt(WhichTree tree, int index_x, int index_y) { | 
| + DCHECK(!needs_tile_swap_); | 
| + | 
| + UpdateToLocalIndex(&index_x, &index_y); | 
| + return TileAtLocalIndex(tree, index_x, index_y); | 
| +} | 
| + | 
| +Tile* TileBundle::TileAtLocalIndex(WhichTree tree, int index_x, int index_y) { | 
| + return tiles_[tree][index_y][index_x].get(); | 
| +} | 
| + | 
| +void TileBundle::SetPriority(WhichTree tree, const TilePriority& priority) { | 
| + DCHECK(!needs_tile_swap_); | 
| + | 
| + if (priority_[tree] == priority) | 
| + return; | 
| + | 
| + priority_[tree] = priority; | 
| + tile_manager_->DidChangeTileBundlePriority(this); | 
| +} | 
| + | 
| +TilePriority TileBundle::GetPriority(WhichTree tree) const { | 
| + return priority_[tree]; | 
| +} | 
| + | 
| +bool TileBundle::RemoveTileAt(WhichTree tree, int index_x, int index_y) { | 
| + DCHECK(!needs_tile_swap_); | 
| + | 
| + UpdateToLocalIndex(&index_x, &index_y); | 
| + bool removed = !!tiles_[tree][index_y][index_x]; | 
| + tiles_[tree][index_y][index_x] = NULL; | 
| + if (removed) | 
| + --conservative_count_; | 
| + return removed; | 
| +} | 
| + | 
| +void TileBundle::AddTileAt(WhichTree tree, | 
| + int index_x, | 
| + int index_y, | 
| + scoped_refptr<Tile> tile) { | 
| + DCHECK(!needs_tile_swap_); | 
| + | 
| + UpdateToLocalIndex(&index_x, &index_y); | 
| + DCHECK(!tiles_[tree][index_y][index_x]); | 
| + tiles_[tree][index_y][index_x] = tile; | 
| + ++conservative_count_; | 
| +} | 
| + | 
| +void TileBundle::DidBecomeRecycled() { | 
| + priority_[ACTIVE_TREE] = TilePriority(); | 
| + needs_tile_swap_ = !needs_tile_swap_; | 
| +} | 
| + | 
| +void TileBundle::DidBecomeActive() { | 
| + priority_[ACTIVE_TREE] = priority_[PENDING_TREE]; | 
| + priority_[PENDING_TREE] = TilePriority(); | 
| + tiles_[ACTIVE_TREE].swap(tiles_[PENDING_TREE]); | 
| + needs_tile_swap_ = false; | 
| +} | 
| + | 
| +void TileBundle::UpdateToLocalIndex(int* index_x, int* index_y) { | 
| + DCHECK(index_x); | 
| + DCHECK(index_y); | 
| + | 
| + *index_x -= offset_x_; | 
| + *index_y -= offset_y_; | 
| + | 
| + DCHECK_GE(*index_x, 0); | 
| + DCHECK_GE(*index_y, 0); | 
| + DCHECK_LT(*index_x, width_); | 
| + DCHECK_LT(*index_x, height_); | 
| +} | 
| + | 
| +void TileBundle::SwapTilesIfRequired() { | 
| + if (!needs_tile_swap_) | 
| + return; | 
| + | 
| + std::swap(priority_[ACTIVE_TREE], priority_[PENDING_TREE]); | 
| + tiles_[ACTIVE_TREE].swap(tiles_[PENDING_TREE]); | 
| + needs_tile_swap_ = false; | 
| +} | 
| + | 
| +TileBundle::Iterator::Iterator(TileBundle* bundle) | 
| + : bundle_(bundle), | 
| + current_tile_(NULL), | 
| + index_x_(0), | 
| + index_y_(0), | 
| + current_tree_(ACTIVE_TREE) { | 
| + trees_to_iterate_.push_back(ACTIVE_TREE); | 
| + trees_to_iterate_.push_back(PENDING_TREE); | 
| + if (!InitializeNewTileForTree(trees_to_iterate_[0])) | 
| + ++(*this); | 
| +} | 
| + | 
| +TileBundle::Iterator::Iterator(TileBundle* bundle, WhichTree tree) | 
| + : bundle_(bundle), | 
| + current_tile_(NULL), | 
| + index_x_(0), | 
| + index_y_(0), | 
| + current_tree_(tree) { | 
| + trees_to_iterate_.push_back(tree); | 
| + if (!InitializeNewTileForTree(trees_to_iterate_[0])) | 
| + ++(*this); | 
| +} | 
| + | 
| +TileBundle::Iterator::~Iterator() {} | 
| + | 
| +TileBundle::Iterator& TileBundle::Iterator::operator++() { | 
| + DCHECK_GT(trees_to_iterate_.size(), 0u); | 
| + DCHECK_LE(trees_to_iterate_.size(), 2u); | 
| + | 
| + if (current_tree_ == trees_to_iterate_[0] && | 
| + trees_to_iterate_.size() > 1 && | 
| + InitializeNewTileForTree(trees_to_iterate_[1])) | 
| 
 
reveman
2013/12/03 19:58:19
Could this be integrated in the loop below instead
 
vmpstr
2013/12/03 22:01:36
Done. I rewrote this a bit.
 
 | 
| + return *this; | 
| + | 
| + do { | 
| + ++index_x_; | 
| + if (index_x_ >= bundle_->width_) { | 
| + index_x_ = 0; | 
| + ++index_y_; | 
| + } | 
| + } while (index_y_ < bundle_->height_ && | 
| + !InitializeNewTileForTree(trees_to_iterate_[0]) && | 
| + (trees_to_iterate_.size() == 1 || | 
| + !InitializeNewTileForTree(trees_to_iterate_[1]))); | 
| 
 
reveman
2013/12/03 19:58:19
I find this code a bit hard to understand. Could s
 
vmpstr
2013/12/03 22:01:36
Done.
 
 | 
| + return *this; | 
| +} | 
| + | 
| +bool TileBundle::Iterator::InitializeNewTileForTree(WhichTree tree) { | 
| + Tile* tile = bundle_->TileAtLocalIndex(tree, index_x_, index_y_); | 
| + if (!tile || tile == current_tile_) | 
| 
 
reveman
2013/12/03 19:58:19
is the "tile == current_tile_" check needed? would
 
vmpstr
2013/12/03 22:01:36
We need this to make sure that the iterator doesn'
 
reveman
2013/12/03 23:13:10
Got it. Thanks for adding the comment.
 
 | 
| + return false; | 
| + | 
| + current_tree_ = tree; | 
| + current_tile_ = tile; | 
| + return true; | 
| +} | 
| + | 
| +} // namespace cc |