OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/eviction_tile_priority_queue.h" | |
6 | |
7 namespace cc { | |
8 | |
9 namespace { | |
10 | |
11 class EvictionOrderComparator { | |
12 public: | |
13 explicit EvictionOrderComparator(TreePriority tree_priority) | |
14 : tree_priority_(tree_priority) {} | |
15 | |
16 bool operator()( | |
17 const EvictionTilePriorityQueue::PairedTilingSetQueue* a, | |
18 const EvictionTilePriorityQueue::PairedTilingSetQueue* b) const { | |
19 // Note that in this function, we have to return true if and only if | |
20 // b is strictly lower priority than a. Note that for the sake of | |
21 // completeness, empty queue is considered to have lowest priority. | |
22 if (a->IsEmpty() || b->IsEmpty()) | |
23 return b->IsEmpty() < a->IsEmpty(); | |
24 | |
25 WhichTree a_tree = a->NextTileIteratorTree(); | |
26 const TilingSetEvictionQueue* a_queue = | |
27 a_tree == ACTIVE_TREE ? a->active_queue.get() : a->pending_queue.get(); | |
28 | |
29 WhichTree b_tree = b->NextTileIteratorTree(); | |
30 const TilingSetEvictionQueue* b_queue = | |
31 b_tree == ACTIVE_TREE ? b->active_queue.get() : b->pending_queue.get(); | |
32 | |
33 const Tile* a_tile = a_queue->Top(); | |
34 const Tile* b_tile = b_queue->Top(); | |
35 | |
36 const TilePriority& a_priority = a_tile->combined_priority(); | |
37 const TilePriority& b_priority = b_tile->combined_priority(); | |
38 bool prioritize_low_res = tree_priority_ == SMOOTHNESS_TAKES_PRIORITY; | |
39 | |
40 // If the priority bin differs, b is lower priority if it has the higher | |
41 // priority bin. | |
42 if (a_priority.priority_bin != b_priority.priority_bin) | |
43 return b_priority.priority_bin > a_priority.priority_bin; | |
44 | |
45 // Otherwise if the resolution differs, then the order will be determined by | |
46 // whether we prioritize low res or not. | |
47 // TODO(vmpstr): Remove this when TilePriority is no longer a member of Tile | |
48 // class but instead produced by the iterators. | |
49 if (b_priority.resolution != a_priority.resolution) { | |
50 // Non ideal resolution should be sorted higher than other resolutions. | |
51 if (a_priority.resolution == NON_IDEAL_RESOLUTION) | |
52 return false; | |
53 | |
54 if (b_priority.resolution == NON_IDEAL_RESOLUTION) | |
55 return true; | |
56 | |
57 if (prioritize_low_res) | |
58 return a_priority.resolution == LOW_RESOLUTION; | |
59 return a_priority.resolution == HIGH_RESOLUTION; | |
60 } | |
61 | |
62 // Otherwise if the occlusion differs, b is lower priority if it is | |
63 // occluded. | |
64 bool a_is_occluded = a_tile->is_occluded_combined(); | |
65 bool b_is_occluded = b_tile->is_occluded_combined(); | |
66 if (a_is_occluded != b_is_occluded) | |
67 return b_is_occluded; | |
68 | |
69 // b is lower priorty if it is farther from visible. | |
70 return b_priority.distance_to_visible > a_priority.distance_to_visible; | |
71 } | |
72 | |
73 private: | |
74 TreePriority tree_priority_; | |
75 }; | |
76 | |
77 } // namespace | |
78 | |
79 EvictionTilePriorityQueue::EvictionTilePriorityQueue() { | |
80 } | |
81 | |
82 EvictionTilePriorityQueue::~EvictionTilePriorityQueue() { | |
83 } | |
84 | |
85 void EvictionTilePriorityQueue::Build( | |
86 const std::vector<PictureLayerImpl::Pair>& paired_layers, | |
87 TreePriority tree_priority) { | |
88 tree_priority_ = tree_priority; | |
89 | |
90 for (std::vector<PictureLayerImpl::Pair>::const_iterator it = | |
91 paired_layers.begin(); | |
92 it != paired_layers.end(); | |
93 ++it) { | |
94 paired_queues_.push_back( | |
95 make_scoped_ptr(new PairedTilingSetQueue(*it, tree_priority_))); | |
96 } | |
97 | |
98 paired_queues_.make_heap(EvictionOrderComparator(tree_priority_)); | |
99 } | |
100 | |
101 bool EvictionTilePriorityQueue::IsEmpty() const { | |
102 return paired_queues_.empty() || paired_queues_.front()->IsEmpty(); | |
103 } | |
104 | |
105 Tile* EvictionTilePriorityQueue::Top() { | |
106 DCHECK(!IsEmpty()); | |
107 return paired_queues_.front()->Top(); | |
108 } | |
109 | |
110 void EvictionTilePriorityQueue::Pop() { | |
111 DCHECK(!IsEmpty()); | |
112 | |
113 paired_queues_.pop_heap(EvictionOrderComparator(tree_priority_)); | |
114 PairedTilingSetQueue* paired_queue = paired_queues_.back(); | |
115 paired_queue->Pop(); | |
116 paired_queues_.push_heap(EvictionOrderComparator(tree_priority_)); | |
117 } | |
118 | |
119 EvictionTilePriorityQueue::PairedTilingSetQueue::PairedTilingSetQueue() { | |
120 } | |
121 | |
122 EvictionTilePriorityQueue::PairedTilingSetQueue::PairedTilingSetQueue( | |
123 const PictureLayerImpl::Pair& layer_pair, | |
124 TreePriority tree_priority) { | |
125 bool skip_shared_out_of_order_tiles = layer_pair.active && layer_pair.pending; | |
126 if (layer_pair.active) { | |
127 active_queue = make_scoped_ptr(new TilingSetEvictionQueue( | |
128 layer_pair.active->picture_layer_tiling_set(), tree_priority, | |
129 skip_shared_out_of_order_tiles)); | |
130 } | |
131 if (layer_pair.pending) { | |
132 pending_queue = make_scoped_ptr(new TilingSetEvictionQueue( | |
133 layer_pair.pending->picture_layer_tiling_set(), tree_priority, | |
134 skip_shared_out_of_order_tiles)); | |
135 } | |
136 } | |
137 | |
138 EvictionTilePriorityQueue::PairedTilingSetQueue::~PairedTilingSetQueue() { | |
139 } | |
140 | |
141 bool EvictionTilePriorityQueue::PairedTilingSetQueue::IsEmpty() const { | |
142 return (!active_queue || active_queue->IsEmpty()) && | |
143 (!pending_queue || pending_queue->IsEmpty()); | |
144 } | |
145 | |
146 Tile* EvictionTilePriorityQueue::PairedTilingSetQueue::Top() { | |
147 DCHECK(!IsEmpty()); | |
148 | |
149 WhichTree next_tree = NextTileIteratorTree(); | |
150 TilingSetEvictionQueue* next_queue = | |
151 next_tree == ACTIVE_TREE ? active_queue.get() : pending_queue.get(); | |
152 DCHECK(next_queue && !next_queue->IsEmpty()); | |
153 | |
154 Tile* tile = next_queue->Top(); | |
155 DCHECK(returned_tiles_for_debug.find(tile) == returned_tiles_for_debug.end()); | |
156 return tile; | |
157 } | |
158 | |
159 void EvictionTilePriorityQueue::PairedTilingSetQueue::Pop() { | |
160 DCHECK(!IsEmpty()); | |
161 | |
162 WhichTree next_tree = NextTileIteratorTree(); | |
163 TilingSetEvictionQueue* next_queue = | |
164 next_tree == ACTIVE_TREE ? active_queue.get() : pending_queue.get(); | |
165 DCHECK(next_queue && !next_queue->IsEmpty()); | |
166 DCHECK(returned_tiles_for_debug.insert(next_queue->Top()).second); | |
167 next_queue->Pop(); | |
168 | |
169 // If not empty, use Top to DCHECK the next iterator. | |
170 DCHECK_IMPLIES(!IsEmpty(), Top()); | |
171 } | |
172 | |
173 WhichTree | |
174 EvictionTilePriorityQueue::PairedTilingSetQueue::NextTileIteratorTree() const { | |
175 DCHECK(!IsEmpty()); | |
176 | |
177 // If we only have one iterator with tiles, return it. | |
178 if (!active_queue || active_queue->IsEmpty()) | |
179 return PENDING_TREE; | |
180 if (!pending_queue || pending_queue->IsEmpty()) | |
181 return ACTIVE_TREE; | |
182 | |
183 const Tile* active_tile = active_queue->Top(); | |
184 const Tile* pending_tile = pending_queue->Top(); | |
185 | |
186 // If tiles are the same, it doesn't matter which tree we return. | |
187 if (active_tile == pending_tile) | |
188 return ACTIVE_TREE; | |
189 | |
190 const TilePriority& active_priority = active_tile->combined_priority(); | |
191 const TilePriority& pending_priority = pending_tile->combined_priority(); | |
192 | |
193 // If the bins are the same and activation differs, then return the tree of | |
194 // the tile not required for activation. | |
195 if (active_priority.priority_bin == pending_priority.priority_bin && | |
196 active_tile->required_for_activation() != | |
197 pending_tile->required_for_activation()) { | |
198 return active_tile->required_for_activation() ? PENDING_TREE : ACTIVE_TREE; | |
199 } | |
200 | |
201 // Return tile with a lower priority. | |
202 if (pending_priority.IsHigherPriorityThan(active_priority)) | |
203 return ACTIVE_TREE; | |
204 return PENDING_TREE; | |
205 } | |
206 | |
207 } // namespace cc | |
OLD | NEW |