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()(EvictionTilePriorityQueue::PairedPictureLayerQueue& a, |
| 17 EvictionTilePriorityQueue::PairedPictureLayerQueue& b) const { |
| 18 if (a.IsEmpty()) |
| 19 return true; |
| 20 |
| 21 if (b.IsEmpty()) |
| 22 return false; |
| 23 |
| 24 PictureLayerImpl::LayerEvictionTileIterator* a_iterator = |
| 25 a.NextTileIterator(tree_priority_); |
| 26 PictureLayerImpl::LayerEvictionTileIterator* b_iterator = |
| 27 b.NextTileIterator(tree_priority_); |
| 28 |
| 29 Tile* a_tile = **a_iterator; |
| 30 Tile* b_tile = **b_iterator; |
| 31 |
| 32 const TilePriority& a_priority = |
| 33 a_tile->priority_for_tree_priority(tree_priority_); |
| 34 const TilePriority& b_priority = |
| 35 b_tile->priority_for_tree_priority(tree_priority_); |
| 36 bool prioritize_low_res = tree_priority_ == SMOOTHNESS_TAKES_PRIORITY; |
| 37 |
| 38 // Now we have to return true iff b is lower priority than a. |
| 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 |
| 60 return a_priority.resolution == HIGH_RESOLUTION; |
| 61 } |
| 62 |
| 63 // Otherwise if the occlusion differs, b is lower priority if it is |
| 64 // occluded. |
| 65 bool a_is_occluded = a_tile->is_occluded_for_tree_priority(tree_priority_); |
| 66 bool b_is_occluded = b_tile->is_occluded_for_tree_priority(tree_priority_); |
| 67 if (a_is_occluded != b_is_occluded) |
| 68 return b_is_occluded; |
| 69 |
| 70 // b is lower priorty if it is farther from visible. |
| 71 return b_priority.distance_to_visible > a_priority.distance_to_visible; |
| 72 } |
| 73 |
| 74 private: |
| 75 TreePriority tree_priority_; |
| 76 }; |
| 77 |
| 78 } // namespace |
| 79 |
| 80 EvictionTilePriorityQueue::EvictionTilePriorityQueue() { |
| 81 } |
| 82 |
| 83 EvictionTilePriorityQueue::~EvictionTilePriorityQueue() { |
| 84 } |
| 85 |
| 86 void EvictionTilePriorityQueue::Build( |
| 87 const std::vector<PictureLayerImpl::Pair>& paired_layers, |
| 88 TreePriority tree_priority) { |
| 89 tree_priority_ = tree_priority; |
| 90 |
| 91 for (std::vector<PictureLayerImpl::Pair>::const_iterator it = |
| 92 paired_layers.begin(); |
| 93 it != paired_layers.end(); |
| 94 ++it) { |
| 95 paired_queues_.push_back(PairedPictureLayerQueue(*it, tree_priority_)); |
| 96 } |
| 97 |
| 98 std::make_heap(paired_queues_.begin(), |
| 99 paired_queues_.end(), |
| 100 EvictionOrderComparator(tree_priority_)); |
| 101 } |
| 102 |
| 103 void EvictionTilePriorityQueue::Reset() { |
| 104 paired_queues_.clear(); |
| 105 } |
| 106 |
| 107 bool EvictionTilePriorityQueue::IsEmpty() const { |
| 108 return paired_queues_.empty() || paired_queues_.front().IsEmpty(); |
| 109 } |
| 110 |
| 111 Tile* EvictionTilePriorityQueue::Top() { |
| 112 DCHECK(!IsEmpty()); |
| 113 return paired_queues_.front().Top(tree_priority_); |
| 114 } |
| 115 |
| 116 void EvictionTilePriorityQueue::Pop() { |
| 117 DCHECK(!IsEmpty()); |
| 118 |
| 119 std::pop_heap(paired_queues_.begin(), |
| 120 paired_queues_.end(), |
| 121 EvictionOrderComparator(tree_priority_)); |
| 122 PairedPictureLayerQueue& paired_queue = paired_queues_.back(); |
| 123 paired_queue.Pop(tree_priority_); |
| 124 std::push_heap(paired_queues_.begin(), |
| 125 paired_queues_.end(), |
| 126 EvictionOrderComparator(tree_priority_)); |
| 127 } |
| 128 |
| 129 EvictionTilePriorityQueue::PairedPictureLayerQueue::PairedPictureLayerQueue() { |
| 130 } |
| 131 |
| 132 EvictionTilePriorityQueue::PairedPictureLayerQueue::PairedPictureLayerQueue( |
| 133 const PictureLayerImpl::Pair& layer_pair, |
| 134 TreePriority tree_priority) |
| 135 : active_iterator( |
| 136 layer_pair.active |
| 137 ? PictureLayerImpl::LayerEvictionTileIterator(layer_pair.active, |
| 138 tree_priority) |
| 139 : PictureLayerImpl::LayerEvictionTileIterator()), |
| 140 pending_iterator( |
| 141 layer_pair.pending |
| 142 ? PictureLayerImpl::LayerEvictionTileIterator(layer_pair.pending, |
| 143 tree_priority) |
| 144 : PictureLayerImpl::LayerEvictionTileIterator()) { |
| 145 } |
| 146 |
| 147 EvictionTilePriorityQueue::PairedPictureLayerQueue::~PairedPictureLayerQueue() { |
| 148 } |
| 149 |
| 150 bool EvictionTilePriorityQueue::PairedPictureLayerQueue::IsEmpty() const { |
| 151 return !active_iterator && !pending_iterator; |
| 152 } |
| 153 |
| 154 Tile* EvictionTilePriorityQueue::PairedPictureLayerQueue::Top( |
| 155 TreePriority tree_priority) { |
| 156 DCHECK(!IsEmpty()); |
| 157 |
| 158 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = |
| 159 NextTileIterator(tree_priority); |
| 160 DCHECK(*next_iterator); |
| 161 |
| 162 Tile* tile = **next_iterator; |
| 163 DCHECK(std::find(returned_shared_tiles.begin(), |
| 164 returned_shared_tiles.end(), |
| 165 tile) == returned_shared_tiles.end()); |
| 166 return tile; |
| 167 } |
| 168 |
| 169 void EvictionTilePriorityQueue::PairedPictureLayerQueue::Pop( |
| 170 TreePriority tree_priority) { |
| 171 DCHECK(!IsEmpty()); |
| 172 |
| 173 PictureLayerImpl::LayerEvictionTileIterator* next_iterator = |
| 174 NextTileIterator(tree_priority); |
| 175 DCHECK(*next_iterator); |
| 176 returned_shared_tiles.push_back(**next_iterator); |
| 177 ++(*next_iterator); |
| 178 |
| 179 if (IsEmpty()) |
| 180 return; |
| 181 |
| 182 next_iterator = NextTileIterator(tree_priority); |
| 183 while (std::find(returned_shared_tiles.begin(), |
| 184 returned_shared_tiles.end(), |
| 185 **next_iterator) != returned_shared_tiles.end()) { |
| 186 ++(*next_iterator); |
| 187 if (IsEmpty()) |
| 188 break; |
| 189 next_iterator = NextTileIterator(tree_priority); |
| 190 } |
| 191 } |
| 192 |
| 193 PictureLayerImpl::LayerEvictionTileIterator* |
| 194 EvictionTilePriorityQueue::PairedPictureLayerQueue::NextTileIterator( |
| 195 TreePriority tree_priority) { |
| 196 DCHECK(!IsEmpty()); |
| 197 |
| 198 // If we only have one iterator with tiles, return it. |
| 199 if (!active_iterator) |
| 200 return &pending_iterator; |
| 201 if (!pending_iterator) |
| 202 return &active_iterator; |
| 203 |
| 204 Tile* active_tile = *active_iterator; |
| 205 Tile* pending_tile = *pending_iterator; |
| 206 if (active_tile == pending_tile) |
| 207 return &active_iterator; |
| 208 |
| 209 const TilePriority& active_priority = |
| 210 active_tile->priority_for_tree_priority(tree_priority); |
| 211 const TilePriority& pending_priority = |
| 212 pending_tile->priority_for_tree_priority(tree_priority); |
| 213 |
| 214 if (pending_priority.IsHigherPriorityThan(active_priority)) |
| 215 return &active_iterator; |
| 216 return &pending_iterator; |
| 217 } |
| 218 |
| 219 } // namespace cc |
OLD | NEW |