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..e986b82465457cb0aac4f6255254469d625d7d9c | 
| --- /dev/null | 
| +++ b/cc/resources/tile_bundle.cc | 
| @@ -0,0 +1,174 @@ | 
| +// 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, gfx::Rect bundle_rect) | 
| + : RefCountedManaged<TileBundle>(tile_manager), | 
| + tile_manager_(tile_manager), | 
| + bundle_rect_(bundle_rect), | 
| + conservative_count_(0u), | 
| + needs_tile_swap_(false), | 
| + id_(s_next_id_++) { | 
| + DCHECK(!bundle_rect_.IsEmpty()); | 
| + | 
| + tiles_[ACTIVE_TREE].resize(bundle_rect_.height()); | 
| + for (int i = 0; i < bundle_rect_.height(); ++i) | 
| + tiles_[ACTIVE_TREE][i].resize(bundle_rect_.width()); | 
| + | 
| + tiles_[PENDING_TREE].resize(bundle_rect_.height()); | 
| + for (int i = 0; i < bundle_rect_.height(); ++i) | 
| + tiles_[PENDING_TREE][i].resize(bundle_rect_.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 -= bundle_rect_.x(); | 
| 
 
enne (OOO)
2013/11/27 01:38:05
Is bundle_rect just a collection of tile indices o
 
vmpstr
2013/11/27 21:09:36
Done.
 
 | 
| + *index_y -= bundle_rect_.y(); | 
| + | 
| + DCHECK_GE(*index_x, 0); | 
| + DCHECK_GE(*index_y, 0); | 
| + DCHECK_LT(*index_x, bundle_rect_.width()); | 
| + DCHECK_LT(*index_x, bundle_rect_.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), | 
| + active_tree_only_tile_(false), | 
| + pending_tree_only_tile_(false) { | 
| + for (int i = 0; i < NUM_TREES; ++i) | 
| + priority_[i] = NULL; | 
| + | 
| + if (!InitializeNewTileForTree(ACTIVE_TREE)) | 
| + ++(*this); | 
| +} | 
| + | 
| +TileBundle::Iterator::~Iterator() {} | 
| + | 
| +TileBundle::Iterator& TileBundle::Iterator::operator++() { | 
| + if (current_tree_ == ACTIVE_TREE && InitializeNewTileForTree(PENDING_TREE)) | 
| + return *this; | 
| + | 
| + do { | 
| + ++index_x_; | 
| + if (index_x_ >= bundle_->bundle_rect_.width()) { | 
| + index_x_ = 0; | 
| + ++index_y_; | 
| + } | 
| + } while (index_y_ < bundle_->bundle_rect_.height() && | 
| + !InitializeNewTileForTree(ACTIVE_TREE) && | 
| + !InitializeNewTileForTree(PENDING_TREE)); | 
| + return *this; | 
| +} | 
| + | 
| +bool TileBundle::Iterator::InitializeNewTileForTree(WhichTree tree) { | 
| + Tile* tile = bundle_->TileAtLocalIndex(tree, index_x_, index_y_); | 
| + if (!tile || tile == current_tile_) | 
| + return false; | 
| + | 
| + current_tree_ = tree; | 
| + current_tile_ = tile; | 
| + priority_[tree] = &bundle_->priority_[tree]; | 
| + | 
| + WhichTree twin_tree = (tree == ACTIVE_TREE) ? PENDING_TREE : ACTIVE_TREE; | 
| + Tile* twin_tile = bundle_->TileAtLocalIndex(twin_tree, index_x_, index_y_); | 
| + if (twin_tile == tile) { | 
| + priority_[twin_tree] = &bundle_->priority_[twin_tree]; | 
| + active_tree_only_tile_ = false; | 
| + pending_tree_only_tile_ = false; | 
| + } else { | 
| + active_tree_only_tile_ = (tree == ACTIVE_TREE); | 
| + pending_tree_only_tile_ = (tree == PENDING_TREE); | 
| + } | 
| + return true; | 
| +} | 
| + | 
| +} // namespace cc |