Home | History | Annotate | Download | only in resources
      1 // Copyright 2013 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/prioritized_tile_set.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "cc/resources/managed_tile_state.h"
     10 #include "cc/resources/tile.h"
     11 
     12 namespace cc {
     13 
     14 class BinComparator {
     15  public:
     16   bool operator()(const scoped_refptr<Tile>& a,
     17                   const scoped_refptr<Tile>& b) const {
     18     const ManagedTileState& ams = a->managed_state();
     19     const ManagedTileState& bms = b->managed_state();
     20 
     21     if (ams.bin[LOW_PRIORITY_BIN] != bms.bin[LOW_PRIORITY_BIN])
     22       return ams.bin[LOW_PRIORITY_BIN] < bms.bin[LOW_PRIORITY_BIN];
     23 
     24     if (ams.required_for_activation != bms.required_for_activation)
     25       return ams.required_for_activation;
     26 
     27     if (ams.resolution != bms.resolution)
     28       return ams.resolution < bms.resolution;
     29 
     30     if (ams.time_to_needed_in_seconds !=  bms.time_to_needed_in_seconds)
     31       return ams.time_to_needed_in_seconds < bms.time_to_needed_in_seconds;
     32 
     33     if (ams.distance_to_visible_in_pixels !=
     34         bms.distance_to_visible_in_pixels) {
     35       return ams.distance_to_visible_in_pixels <
     36              bms.distance_to_visible_in_pixels;
     37     }
     38 
     39     gfx::Rect a_rect = a->content_rect();
     40     gfx::Rect b_rect = b->content_rect();
     41     if (a_rect.y() != b_rect.y())
     42       return a_rect.y() < b_rect.y();
     43     return a_rect.x() < b_rect.x();
     44   }
     45 };
     46 
     47 namespace {
     48 
     49 typedef std::vector<scoped_refptr<Tile> > TileVector;
     50 
     51 void SortBinTiles(ManagedTileBin bin, TileVector* tiles) {
     52   switch (bin) {
     53     case NOW_AND_READY_TO_DRAW_BIN:
     54       break;
     55     case NOW_BIN:
     56     case SOON_BIN:
     57     case EVENTUALLY_AND_ACTIVE_BIN:
     58     case EVENTUALLY_BIN:
     59     case NEVER_AND_ACTIVE_BIN:
     60     case NEVER_BIN:
     61       std::sort(tiles->begin(), tiles->end(), BinComparator());
     62       break;
     63     default:
     64       NOTREACHED();
     65   }
     66 }
     67 
     68 }  // namespace
     69 
     70 PrioritizedTileSet::PrioritizedTileSet() {}
     71 
     72 PrioritizedTileSet::~PrioritizedTileSet() {}
     73 
     74 void PrioritizedTileSet::InsertTile(Tile* tile, ManagedTileBin bin) {
     75   tiles_[bin].push_back(make_scoped_refptr(tile));
     76 }
     77 
     78 void PrioritizedTileSet::Clear() {
     79   for (int bin = 0; bin < NUM_BINS; ++bin)
     80     tiles_[bin].clear();
     81 }
     82 
     83 void PrioritizedTileSet::Sort() {
     84   for (int bin = 0; bin < NUM_BINS; ++bin)
     85     SortBinTiles(static_cast<ManagedTileBin>(bin), &tiles_[bin]);
     86 }
     87 
     88 PrioritizedTileSet::PriorityIterator::PriorityIterator(
     89     PrioritizedTileSet* tile_set)
     90     : tile_set_(tile_set),
     91       current_bin_(NOW_AND_READY_TO_DRAW_BIN),
     92       iterator_(tile_set->tiles_[current_bin_].begin()) {
     93   if (iterator_ == tile_set_->tiles_[current_bin_].end())
     94     AdvanceList();
     95 }
     96 
     97 PrioritizedTileSet::PriorityIterator::~PriorityIterator() {}
     98 
     99 PrioritizedTileSet::PriorityIterator&
    100 PrioritizedTileSet::PriorityIterator::operator++() {
    101   // We can't increment past the end of the tiles.
    102   DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());
    103 
    104   ++iterator_;
    105   if (iterator_ == tile_set_->tiles_[current_bin_].end())
    106     AdvanceList();
    107   return *this;
    108 }
    109 
    110 Tile* PrioritizedTileSet::PriorityIterator::operator*() {
    111   DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());
    112   return iterator_->get();
    113 }
    114 
    115 void PrioritizedTileSet::PriorityIterator::AdvanceList() {
    116   DCHECK(iterator_ == tile_set_->tiles_[current_bin_].end());
    117 
    118   while (current_bin_ != NEVER_BIN) {
    119     current_bin_ = static_cast<ManagedTileBin>(current_bin_ + 1);
    120     iterator_ = tile_set_->tiles_[current_bin_].begin();
    121     if (iterator_ != tile_set_->tiles_[current_bin_].end())
    122       break;
    123   }
    124 }
    125 
    126 }  // namespace cc
    127