Chromium Code Reviews| 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_iterators_.clear(); | |
| 19 iterator_heap_.clear(); | |
| 20 } | |
| 21 | |
| 22 void EvictionTilePriorityQueue::BuildQueue( | |
| 23 const std::vector<PairedPictureLayer>& paired_picture_layers, | |
| 24 TreePriority tree_priority) { | |
| 25 DCHECK(paired_iterators_.empty()); | |
| 26 DCHECK(iterator_heap_.empty()); | |
| 27 paired_picture_layers_ = paired_picture_layers; | |
|
reveman
2014/07/15 01:51:10
Hm, you're making a copy of a large vector here. I
vmpstr
2014/07/15 05:36:26
We can pass the raw layers vector and build it, if
reveman
2014/07/15 16:49:56
Keeping a raw pointer around and assuming that it'
vmpstr
2014/07/15 17:47:20
I meant we can pass in const std::vector<PictureLa
| |
| 28 tree_priority_ = tree_priority; | |
| 29 comparator_ = EvictionOrderComparator(tree_priority); | |
| 30 initialized_ = false; | |
| 31 } | |
| 32 | |
| 33 void EvictionTilePriorityQueue::Initialize() { | |
| 34 paired_iterators_.reserve(paired_picture_layers_.size()); | |
| 35 iterator_heap_.reserve(paired_picture_layers_.size()); | |
|
reveman
2014/07/15 01:51:09
Let's not bother with reserve() when this only gro
vmpstr
2014/07/15 05:36:26
That knowledge is not obvious in this class, but s
| |
| 36 for (std::vector<PairedPictureLayer>::iterator it = | |
| 37 paired_picture_layers_.begin(); | |
| 38 it != paired_picture_layers_.end(); | |
| 39 ++it) { | |
| 40 PairedPictureLayerIterator paired_iterator; | |
| 41 if (it->active_layer) { | |
| 42 paired_iterator.active_iterator = | |
| 43 PictureLayerImpl::LayerEvictionTileIterator(it->active_layer, | |
| 44 tree_priority_); | |
| 45 } | |
| 46 | |
| 47 if (it->pending_layer) { | |
| 48 paired_iterator.pending_iterator = | |
| 49 PictureLayerImpl::LayerEvictionTileIterator(it->pending_layer, | |
| 50 tree_priority_); | |
| 51 } | |
| 52 | |
| 53 if (paired_iterator.PeekTile(tree_priority_) != NULL) { | |
| 54 paired_iterators_.push_back(paired_iterator); | |
| 55 iterator_heap_.push_back(&paired_iterators_.back()); | |
| 56 } | |
| 57 } | |
|
reveman
2014/07/15 01:51:10
How much of the cost of Initialize is building the
vmpstr
2014/07/15 05:36:26
The vector building here is the expensive part. Th
reveman
2014/07/15 16:49:56
Could most of the ctor cost be moved to PeekTile i
vmpstr
2014/07/15 17:47:20
PeekTile will find a tiling iterator and access it
reveman
2014/07/15 19:44:35
Hm, looks like we already have some lazy init stuf
| |
| 58 | |
| 59 std::make_heap(iterator_heap_.begin(), iterator_heap_.end(), comparator_); | |
| 60 initialized_ = true; | |
| 61 } | |
| 62 | |
| 63 EvictionTilePriorityQueue::~EvictionTilePriorityQueue() { | |
| 64 } | |
| 65 | |
| 66 void EvictionTilePriorityQueue::Pop() { | |
| 67 if (!initialized_) | |
| 68 Initialize(); | |
| 69 | |
| 70 std::pop_heap(iterator_heap_.begin(), iterator_heap_.end(), comparator_); | |
| 71 PairedPictureLayerIterator* paired_iterator = iterator_heap_.back(); | |
| 72 iterator_heap_.pop_back(); | |
| 73 | |
| 74 paired_iterator->PopTile(tree_priority_); | |
| 75 if (paired_iterator->PeekTile(tree_priority_) != NULL) { | |
| 76 iterator_heap_.push_back(paired_iterator); | |
| 77 std::push_heap(iterator_heap_.begin(), iterator_heap_.end(), comparator_); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 bool EvictionTilePriorityQueue::IsEmpty() { | |
| 82 if (!initialized_) | |
| 83 Initialize(); | |
| 84 | |
| 85 return iterator_heap_.empty(); | |
| 86 } | |
| 87 | |
| 88 Tile* EvictionTilePriorityQueue::Top() { | |
| 89 if (!initialized_) | |
| 90 Initialize(); | |
| 91 | |
| 92 DCHECK(!IsEmpty()); | |
| 93 return iterator_heap_.front()->PeekTile(tree_priority_); | |
| 94 } | |
| 95 | |
| 96 EvictionTilePriorityQueue::PairedPictureLayerIterator:: | |
| 97 PairedPictureLayerIterator() { | |
| 98 } | |
| 99 | |
| 100 EvictionTilePriorityQueue::PairedPictureLayerIterator:: | |
| 101 ~PairedPictureLayerIterator() { | |
| 102 } | |
| 103 | |
| 104 Tile* EvictionTilePriorityQueue::PairedPictureLayerIterator::PeekTile( | |
| 105 TreePriority tree_priority) { | |
| 106 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = | |
| 107 NextTileIterator(tree_priority); | |
| 108 if (!next_iterator) | |
| 109 return NULL; | |
| 110 | |
| 111 DCHECK(*next_iterator); | |
| 112 DCHECK(std::find(returned_shared_tiles.begin(), | |
| 113 returned_shared_tiles.end(), | |
| 114 **next_iterator) == returned_shared_tiles.end()); | |
| 115 return **next_iterator; | |
| 116 } | |
| 117 | |
| 118 void EvictionTilePriorityQueue::PairedPictureLayerIterator::PopTile( | |
| 119 TreePriority tree_priority) { | |
| 120 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = | |
| 121 NextTileIterator(tree_priority); | |
| 122 DCHECK(next_iterator); | |
| 123 DCHECK(*next_iterator); | |
| 124 returned_shared_tiles.push_back(**next_iterator); | |
| 125 ++(*next_iterator); | |
| 126 | |
| 127 next_iterator = NextTileIterator(tree_priority); | |
| 128 while (next_iterator && | |
| 129 std::find(returned_shared_tiles.begin(), | |
| 130 returned_shared_tiles.end(), | |
| 131 **next_iterator) != returned_shared_tiles.end()) { | |
| 132 ++(*next_iterator); | |
| 133 next_iterator = NextTileIterator(tree_priority); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 PictureLayerImpl::LayerEvictionTileIterator* | |
| 138 EvictionTilePriorityQueue::PairedPictureLayerIterator::NextTileIterator( | |
| 139 TreePriority tree_priority) { | |
| 140 // If both iterators are out of tiles, return NULL. | |
| 141 if (!active_iterator && !pending_iterator) | |
| 142 return NULL; | |
| 143 | |
| 144 // If we only have one iterator with tiles, return it. | |
| 145 if (!active_iterator) | |
| 146 return &pending_iterator; | |
| 147 if (!pending_iterator) | |
| 148 return &active_iterator; | |
| 149 | |
| 150 Tile* active_tile = *active_iterator; | |
| 151 Tile* pending_tile = *pending_iterator; | |
| 152 if (active_tile == pending_tile) | |
| 153 return &active_iterator; | |
| 154 | |
| 155 const TilePriority& active_priority = | |
| 156 active_tile->priority_for_tree_priority(tree_priority); | |
| 157 const TilePriority& pending_priority = | |
| 158 pending_tile->priority_for_tree_priority(tree_priority); | |
| 159 | |
| 160 if (pending_priority.IsHigherPriorityThan(active_priority)) | |
| 161 return &active_iterator; | |
| 162 return &pending_iterator; | |
| 163 } | |
| 164 | |
| 165 EvictionTilePriorityQueue::EvictionOrderComparator::EvictionOrderComparator( | |
| 166 TreePriority tree_priority) | |
| 167 : tree_priority_(tree_priority) { | |
| 168 } | |
| 169 | |
| 170 bool EvictionTilePriorityQueue::EvictionOrderComparator::operator()( | |
| 171 PairedPictureLayerIterator* a, | |
| 172 PairedPictureLayerIterator* b) const { | |
| 173 PictureLayerImpl::LayerEvictionTileIterator* a_iterator = | |
| 174 a->NextTileIterator(tree_priority_); | |
| 175 DCHECK(a_iterator); | |
| 176 DCHECK(*a_iterator); | |
| 177 | |
| 178 PictureLayerImpl::LayerEvictionTileIterator* b_iterator = | |
| 179 b->NextTileIterator(tree_priority_); | |
| 180 DCHECK(b_iterator); | |
| 181 DCHECK(*b_iterator); | |
| 182 | |
| 183 Tile* a_tile = **a_iterator; | |
| 184 Tile* b_tile = **b_iterator; | |
| 185 | |
| 186 const TilePriority& a_priority = | |
| 187 a_tile->priority_for_tree_priority(tree_priority_); | |
| 188 const TilePriority& b_priority = | |
| 189 b_tile->priority_for_tree_priority(tree_priority_); | |
| 190 bool prioritize_low_res = tree_priority_ == SMOOTHNESS_TAKES_PRIORITY; | |
| 191 | |
| 192 // Now we have to return true iff b is lower priority than a. | |
| 193 | |
| 194 // If the bin is the same but the resolution is not, then the order will be | |
| 195 // determined by whether we prioritize low res or not. | |
| 196 // TODO(vmpstr): Remove this when TilePriority is no longer a member of Tile | |
| 197 // class but instead produced by the iterators. | |
| 198 if (b_priority.priority_bin == a_priority.priority_bin && | |
| 199 b_priority.resolution != a_priority.resolution) { | |
| 200 // Non ideal resolution should be sorted higher than other resolutions. | |
| 201 if (a_priority.resolution == NON_IDEAL_RESOLUTION) | |
| 202 return false; | |
| 203 | |
| 204 if (b_priority.resolution == NON_IDEAL_RESOLUTION) | |
| 205 return true; | |
| 206 | |
| 207 if (prioritize_low_res) | |
| 208 return a_priority.resolution == LOW_RESOLUTION; | |
| 209 | |
| 210 return a_priority.resolution == HIGH_RESOLUTION; | |
| 211 } | |
| 212 return a_priority.IsHigherPriorityThan(b_priority); | |
| 213 } | |
| 214 | |
| 215 } // namespace cc | |
| OLD | NEW |