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/prioritized_tile_set.h"
6
7 #include <algorithm>
8
9 #include "cc/resources/managed_tile_state.h"
10 #include "cc/resources/tile.h"
11
12 namespace cc {
13
14 class BinComparator {
15 public:
operator ()(const Tile * a,const Tile * b) const16 bool operator()(const Tile* a,
17 const Tile* b) const {
18 const ManagedTileState& ams = a->managed_state();
19 const ManagedTileState& bms = b->managed_state();
20
21 if (ams.required_for_activation != bms.required_for_activation)
22 return ams.required_for_activation;
23
24 if (ams.resolution != bms.resolution)
25 return ams.resolution < bms.resolution;
26
27 if (ams.time_to_needed_in_seconds != bms.time_to_needed_in_seconds)
28 return ams.time_to_needed_in_seconds < bms.time_to_needed_in_seconds;
29
30 if (ams.distance_to_visible_in_pixels !=
31 bms.distance_to_visible_in_pixels) {
32 return ams.distance_to_visible_in_pixels <
33 bms.distance_to_visible_in_pixels;
34 }
35
36 gfx::Rect a_rect = a->content_rect();
37 gfx::Rect b_rect = b->content_rect();
38 if (a_rect.y() != b_rect.y())
39 return a_rect.y() < b_rect.y();
40 return a_rect.x() < b_rect.x();
41 }
42 };
43
44 namespace {
45
46 typedef std::vector<Tile*> TileVector;
47
SortBinTiles(ManagedTileBin bin,TileVector * tiles)48 void SortBinTiles(ManagedTileBin bin, TileVector* tiles) {
49 switch (bin) {
50 case NOW_AND_READY_TO_DRAW_BIN:
51 case NEVER_BIN:
52 break;
53 case NOW_BIN:
54 case SOON_BIN:
55 case EVENTUALLY_AND_ACTIVE_BIN:
56 case EVENTUALLY_BIN:
57 case AT_LAST_AND_ACTIVE_BIN:
58 case AT_LAST_BIN:
59 std::sort(tiles->begin(), tiles->end(), BinComparator());
60 break;
61 default:
62 NOTREACHED();
63 }
64 }
65
66 } // namespace
67
PrioritizedTileSet()68 PrioritizedTileSet::PrioritizedTileSet() {
69 for (int bin = 0; bin < NUM_BINS; ++bin)
70 bin_sorted_[bin] = true;
71 }
72
~PrioritizedTileSet()73 PrioritizedTileSet::~PrioritizedTileSet() {}
74
InsertTile(Tile * tile,ManagedTileBin bin)75 void PrioritizedTileSet::InsertTile(Tile* tile, ManagedTileBin bin) {
76 tiles_[bin].push_back(tile);
77 bin_sorted_[bin] = false;
78 }
79
Clear()80 void PrioritizedTileSet::Clear() {
81 for (int bin = 0; bin < NUM_BINS; ++bin) {
82 tiles_[bin].clear();
83 bin_sorted_[bin] = true;
84 }
85 }
86
SortBinIfNeeded(ManagedTileBin bin)87 void PrioritizedTileSet::SortBinIfNeeded(ManagedTileBin bin) {
88 if (!bin_sorted_[bin]) {
89 SortBinTiles(bin, &tiles_[bin]);
90 bin_sorted_[bin] = true;
91 }
92 }
93
Iterator(PrioritizedTileSet * tile_set,bool use_priority_ordering)94 PrioritizedTileSet::Iterator::Iterator(
95 PrioritizedTileSet* tile_set, bool use_priority_ordering)
96 : tile_set_(tile_set),
97 current_bin_(NOW_AND_READY_TO_DRAW_BIN),
98 use_priority_ordering_(use_priority_ordering) {
99 if (use_priority_ordering_)
100 tile_set_->SortBinIfNeeded(current_bin_);
101 iterator_ = tile_set->tiles_[current_bin_].begin();
102 if (iterator_ == tile_set_->tiles_[current_bin_].end())
103 AdvanceList();
104 }
105
~Iterator()106 PrioritizedTileSet::Iterator::~Iterator() {}
107
DisablePriorityOrdering()108 void PrioritizedTileSet::Iterator::DisablePriorityOrdering() {
109 use_priority_ordering_ = false;
110 }
111
112 PrioritizedTileSet::Iterator&
operator ++()113 PrioritizedTileSet::Iterator::operator++() {
114 // We can't increment past the end of the tiles.
115 DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());
116
117 ++iterator_;
118 if (iterator_ == tile_set_->tiles_[current_bin_].end())
119 AdvanceList();
120 return *this;
121 }
122
operator *()123 Tile* PrioritizedTileSet::Iterator::operator*() {
124 DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());
125 return *iterator_;
126 }
127
AdvanceList()128 void PrioritizedTileSet::Iterator::AdvanceList() {
129 DCHECK(iterator_ == tile_set_->tiles_[current_bin_].end());
130
131 while (current_bin_ != NEVER_BIN) {
132 current_bin_ = static_cast<ManagedTileBin>(current_bin_ + 1);
133
134 if (use_priority_ordering_)
135 tile_set_->SortBinIfNeeded(current_bin_);
136
137 iterator_ = tile_set_->tiles_[current_bin_].begin();
138 if (iterator_ != tile_set_->tiles_[current_bin_].end())
139 break;
140 }
141 }
142
143 } // namespace cc
144