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 #include "cc/resources/tile.h" | |
8 | |
9 namespace cc { | |
10 | |
11 EvictionTilePriorityQueue::EvictionTilePriorityQueue() | |
12 : tree_priority_(SAME_PRIORITY_FOR_BOTH_TREES), | |
13 comparator_(tree_priority_), | |
14 initialized_(true) { | |
15 } | |
16 | |
17 void EvictionTilePriorityQueue::Reset() { | |
18 paired_queues_.clear(); | |
19 queue_heap_.clear(); | |
20 paired_picture_layers_.clear(); | |
21 } | |
22 | |
23 void EvictionTilePriorityQueue::BuildQueue( | |
24 const std::vector<PictureLayerImpl*>& layers, | |
25 TreePriority tree_priority) { | |
26 DCHECK(paired_queues_.empty()); | |
27 DCHECK(queue_heap_.empty()); | |
28 DCHECK(paired_picture_layers_.empty()); | |
29 | |
30 GetPairedPictureLayers(layers, &paired_picture_layers_); | |
reveman
2014/07/15 19:44:37
Sorry, I think this is the wrong approach. Now we'
vmpstr
2014/07/15 20:38:17
I don't think this is too bad. Incremental updates
reveman
2014/07/15 22:31:55
Incremental approach might not be worth it but you
| |
31 tree_priority_ = tree_priority; | |
32 comparator_ = EvictionOrderComparator(tree_priority); | |
33 initialized_ = false; | |
34 } | |
35 | |
36 void EvictionTilePriorityQueue::Initialize() { | |
37 // Since iterator heap uses pointers into paired_queues, we have to reserve | |
38 // the paired_queues to avoid reallocations of the vector. | |
39 paired_queues_.reserve(paired_picture_layers_.size()); | |
reveman
2014/07/15 19:44:37
I don't understand this and the comment doesn't he
vmpstr
2014/07/15 20:38:17
These pointers point to the vector's memory. If pa
reveman
2014/07/15 22:31:55
Ah, I see. That's very subtle. Not using two vecto
| |
40 for (std::vector<PairedPictureLayer>::iterator it = | |
41 paired_picture_layers_.begin(); | |
42 it != paired_picture_layers_.end(); | |
43 ++it) { | |
44 PairedPictureLayerQueue paired_queue; | |
45 if (it->active_layer) { | |
46 paired_queue.active_iterator = | |
47 PictureLayerImpl::LayerEvictionTileIterator(it->active_layer, | |
48 tree_priority_); | |
49 } | |
50 | |
51 if (it->pending_layer) { | |
52 paired_queue.pending_iterator = | |
53 PictureLayerImpl::LayerEvictionTileIterator(it->pending_layer, | |
54 tree_priority_); | |
55 } | |
56 | |
57 if (paired_queue.PeekTile(tree_priority_) != NULL) { | |
58 paired_queues_.push_back(paired_queue); | |
59 queue_heap_.push_back(&paired_queues_.back()); | |
60 } | |
61 } | |
62 | |
63 std::make_heap(queue_heap_.begin(), queue_heap_.end(), comparator_); | |
64 initialized_ = true; | |
65 } | |
66 | |
67 EvictionTilePriorityQueue::~EvictionTilePriorityQueue() { | |
68 } | |
69 | |
70 void EvictionTilePriorityQueue::Pop() { | |
71 if (!initialized_) | |
72 Initialize(); | |
73 | |
74 std::pop_heap(queue_heap_.begin(), queue_heap_.end(), comparator_); | |
75 PairedPictureLayerQueue* paired_queue = queue_heap_.back(); | |
76 queue_heap_.pop_back(); | |
77 | |
78 paired_queue->PopTile(tree_priority_); | |
79 if (paired_queue->PeekTile(tree_priority_) != NULL) { | |
80 queue_heap_.push_back(paired_queue); | |
81 std::push_heap(queue_heap_.begin(), queue_heap_.end(), comparator_); | |
82 } | |
83 } | |
84 | |
85 bool EvictionTilePriorityQueue::IsEmpty() { | |
86 if (!initialized_) | |
87 Initialize(); | |
88 | |
89 return queue_heap_.empty(); | |
90 } | |
91 | |
92 Tile* EvictionTilePriorityQueue::Top() { | |
93 if (!initialized_) | |
94 Initialize(); | |
95 | |
96 DCHECK(!IsEmpty()); | |
97 return queue_heap_.front()->PeekTile(tree_priority_); | |
98 } | |
99 | |
100 EvictionTilePriorityQueue::PairedPictureLayerQueue::PairedPictureLayerQueue() { | |
101 } | |
102 | |
103 EvictionTilePriorityQueue::PairedPictureLayerQueue::~PairedPictureLayerQueue() { | |
104 } | |
105 | |
106 Tile* EvictionTilePriorityQueue::PairedPictureLayerQueue::PeekTile( | |
107 TreePriority tree_priority) { | |
108 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = | |
109 NextTileIterator(tree_priority); | |
110 if (!next_iterator) | |
111 return NULL; | |
112 | |
113 DCHECK(*next_iterator); | |
114 DCHECK(std::find(returned_shared_tiles.begin(), | |
115 returned_shared_tiles.end(), | |
116 **next_iterator) == returned_shared_tiles.end()); | |
117 return **next_iterator; | |
118 } | |
119 | |
120 void EvictionTilePriorityQueue::PairedPictureLayerQueue::PopTile( | |
121 TreePriority tree_priority) { | |
122 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = | |
123 NextTileIterator(tree_priority); | |
124 DCHECK(next_iterator); | |
125 DCHECK(*next_iterator); | |
126 returned_shared_tiles.push_back(**next_iterator); | |
127 ++(*next_iterator); | |
128 | |
129 next_iterator = NextTileIterator(tree_priority); | |
130 while (next_iterator && | |
131 std::find(returned_shared_tiles.begin(), | |
132 returned_shared_tiles.end(), | |
133 **next_iterator) != returned_shared_tiles.end()) { | |
134 ++(*next_iterator); | |
135 next_iterator = NextTileIterator(tree_priority); | |
136 } | |
137 } | |
138 | |
139 PictureLayerImpl::LayerEvictionTileIterator* | |
140 EvictionTilePriorityQueue::PairedPictureLayerQueue::NextTileIterator( | |
141 TreePriority tree_priority) { | |
142 // If both iterators are out of tiles, return NULL. | |
143 if (!active_iterator && !pending_iterator) | |
144 return NULL; | |
145 | |
146 // If we only have one iterator with tiles, return it. | |
147 if (!active_iterator) | |
148 return &pending_iterator; | |
149 if (!pending_iterator) | |
150 return &active_iterator; | |
151 | |
152 Tile* active_tile = *active_iterator; | |
153 Tile* pending_tile = *pending_iterator; | |
154 if (active_tile == pending_tile) | |
155 return &active_iterator; | |
156 | |
157 const TilePriority& active_priority = | |
158 active_tile->priority_for_tree_priority(tree_priority); | |
159 const TilePriority& pending_priority = | |
160 pending_tile->priority_for_tree_priority(tree_priority); | |
161 | |
162 if (pending_priority.IsHigherPriorityThan(active_priority)) | |
163 return &active_iterator; | |
164 return &pending_iterator; | |
165 } | |
166 | |
167 EvictionTilePriorityQueue::EvictionOrderComparator::EvictionOrderComparator( | |
168 TreePriority tree_priority) | |
169 : tree_priority_(tree_priority) { | |
170 } | |
171 | |
172 bool EvictionTilePriorityQueue::EvictionOrderComparator::operator()( | |
173 PairedPictureLayerQueue* a, | |
174 PairedPictureLayerQueue* b) const { | |
175 PictureLayerImpl::LayerEvictionTileIterator* a_iterator = | |
176 a->NextTileIterator(tree_priority_); | |
177 DCHECK(a_iterator); | |
178 DCHECK(*a_iterator); | |
179 | |
180 PictureLayerImpl::LayerEvictionTileIterator* b_iterator = | |
181 b->NextTileIterator(tree_priority_); | |
182 DCHECK(b_iterator); | |
183 DCHECK(*b_iterator); | |
184 | |
185 Tile* a_tile = **a_iterator; | |
186 Tile* b_tile = **b_iterator; | |
187 | |
188 const TilePriority& a_priority = | |
189 a_tile->priority_for_tree_priority(tree_priority_); | |
190 const TilePriority& b_priority = | |
191 b_tile->priority_for_tree_priority(tree_priority_); | |
192 bool prioritize_low_res = tree_priority_ == SMOOTHNESS_TAKES_PRIORITY; | |
193 | |
194 // Now we have to return true iff b is lower priority than a. | |
195 | |
196 // If the bin is the same but the resolution is not, then the order will be | |
197 // determined by whether we prioritize low res or not. | |
198 // TODO(vmpstr): Remove this when TilePriority is no longer a member of Tile | |
199 // class but instead produced by the iterators. | |
200 if (b_priority.priority_bin == a_priority.priority_bin && | |
201 b_priority.resolution != a_priority.resolution) { | |
202 // Non ideal resolution should be sorted higher than other resolutions. | |
203 if (a_priority.resolution == NON_IDEAL_RESOLUTION) | |
204 return false; | |
205 | |
206 if (b_priority.resolution == NON_IDEAL_RESOLUTION) | |
207 return true; | |
208 | |
209 if (prioritize_low_res) | |
210 return a_priority.resolution == LOW_RESOLUTION; | |
211 | |
212 return a_priority.resolution == HIGH_RESOLUTION; | |
213 } | |
214 return a_priority.IsHigherPriorityThan(b_priority); | |
215 } | |
216 | |
217 } // namespace cc | |
OLD | NEW |