summaryrefslogtreecommitdiffstats
path: root/cc/resources/prioritized_tile_set.cc
blob: 82c7f16facdd4567e472e7d262f6f04ded652a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/resources/prioritized_tile_set.h"

#include <algorithm>

#include "cc/resources/managed_tile_state.h"
#include "cc/resources/tile.h"

namespace cc {

class BinComparator {
 public:
  bool operator()(const Tile* a,
                  const Tile* b) const {
    const ManagedTileState& ams = a->managed_state();
    const ManagedTileState& bms = b->managed_state();

    if (ams.priority_bin != bms.priority_bin)
      return ams.priority_bin < bms.priority_bin;

    if (ams.required_for_activation != bms.required_for_activation)
      return ams.required_for_activation;

    if (ams.resolution != bms.resolution)
      return ams.resolution < bms.resolution;

    if (ams.distance_to_visible != bms.distance_to_visible)
      return ams.distance_to_visible < bms.distance_to_visible;

    gfx::Rect a_rect = a->content_rect();
    gfx::Rect b_rect = b->content_rect();
    if (a_rect.y() != b_rect.y())
      return a_rect.y() < b_rect.y();
    return a_rect.x() < b_rect.x();
  }
};

namespace {

typedef std::vector<Tile*> TileVector;

void SortBinTiles(ManagedTileBin bin, TileVector* tiles) {
  switch (bin) {
    case NOW_AND_READY_TO_DRAW_BIN:
    case NEVER_BIN:
      break;
    case NOW_BIN:
    case SOON_BIN:
    case EVENTUALLY_AND_ACTIVE_BIN:
    case EVENTUALLY_BIN:
    case AT_LAST_AND_ACTIVE_BIN:
    case AT_LAST_BIN:
      std::sort(tiles->begin(), tiles->end(), BinComparator());
      break;
    default:
      NOTREACHED();
  }
}

}  // namespace

PrioritizedTileSet::PrioritizedTileSet() {
  for (int bin = 0; bin < NUM_BINS; ++bin)
    bin_sorted_[bin] = true;
}

PrioritizedTileSet::~PrioritizedTileSet() {}

void PrioritizedTileSet::InsertTile(Tile* tile, ManagedTileBin bin) {
  tiles_[bin].push_back(tile);
  bin_sorted_[bin] = false;
}

void PrioritizedTileSet::Clear() {
  for (int bin = 0; bin < NUM_BINS; ++bin) {
    tiles_[bin].clear();
    bin_sorted_[bin] = true;
  }
}

bool PrioritizedTileSet::IsEmpty() {
  for (int bin = 0; bin < NUM_BINS; ++bin)
    if (!tiles_[bin].empty())
      return false;

  return true;
}

void PrioritizedTileSet::SortBinIfNeeded(ManagedTileBin bin) {
  if (!bin_sorted_[bin]) {
    SortBinTiles(bin, &tiles_[bin]);
    bin_sorted_[bin] = true;
  }
}

PrioritizedTileSet::Iterator::Iterator(
    PrioritizedTileSet* tile_set, bool use_priority_ordering)
    : tile_set_(tile_set),
      current_bin_(NOW_AND_READY_TO_DRAW_BIN),
      use_priority_ordering_(use_priority_ordering) {
  if (use_priority_ordering_)
    tile_set_->SortBinIfNeeded(current_bin_);
  iterator_ = tile_set->tiles_[current_bin_].begin();
  if (iterator_ == tile_set_->tiles_[current_bin_].end())
    AdvanceList();
}

PrioritizedTileSet::Iterator::~Iterator() {}

void PrioritizedTileSet::Iterator::DisablePriorityOrdering() {
  use_priority_ordering_ = false;
}

PrioritizedTileSet::Iterator&
PrioritizedTileSet::Iterator::operator++() {
  // We can't increment past the end of the tiles.
  DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());

  ++iterator_;
  if (iterator_ == tile_set_->tiles_[current_bin_].end())
    AdvanceList();
  return *this;
}

Tile* PrioritizedTileSet::Iterator::operator*() {
  DCHECK(iterator_ != tile_set_->tiles_[current_bin_].end());
  return *iterator_;
}

void PrioritizedTileSet::Iterator::AdvanceList() {
  DCHECK(iterator_ == tile_set_->tiles_[current_bin_].end());

  while (current_bin_ != NEVER_BIN) {
    current_bin_ = static_cast<ManagedTileBin>(current_bin_ + 1);

    if (use_priority_ordering_)
      tile_set_->SortBinIfNeeded(current_bin_);

    iterator_ = tile_set_->tiles_[current_bin_].begin();
    if (iterator_ != tile_set_->tiles_[current_bin_].end())
      break;
  }
}

}  // namespace cc