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