[Enhanced Bookmark] Change "Bookmark Bar" to "Bookmark bar" on Android
[chromium-blink-merge.git] / cc / resources / pixel_ref_map.cc
blobdc5085144ea8c7cc3cc32bd670cdb07452428f21
1 // Copyright 2015 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.
5 #include "cc/resources/pixel_ref_map.h"
7 #include <algorithm>
8 #include <limits>
10 #include "cc/base/util.h"
11 #include "cc/resources/picture.h"
12 #include "skia/ext/pixel_ref_utils.h"
14 namespace cc {
16 PixelRefMap::PixelRefMap(const gfx::Size& cell_size) : cell_size_(cell_size) {
17 DCHECK(!cell_size.IsEmpty());
20 PixelRefMap::~PixelRefMap() {
23 void PixelRefMap::GatherPixelRefsFromPicture(SkPicture* picture) {
24 DCHECK(picture);
26 int min_x = std::numeric_limits<int>::max();
27 int min_y = std::numeric_limits<int>::max();
28 int max_x = 0;
29 int max_y = 0;
31 skia::DiscardablePixelRefList pixel_refs;
32 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture, &pixel_refs);
33 for (skia::DiscardablePixelRefList::const_iterator it = pixel_refs.begin();
34 it != pixel_refs.end(); ++it) {
35 gfx::Point min(
36 RoundDown(static_cast<int>(it->pixel_ref_rect.x()), cell_size_.width()),
37 RoundDown(static_cast<int>(it->pixel_ref_rect.y()),
38 cell_size_.height()));
39 gfx::Point max(
40 RoundDown(static_cast<int>(std::ceil(it->pixel_ref_rect.right())),
41 cell_size_.width()),
42 RoundDown(static_cast<int>(std::ceil(it->pixel_ref_rect.bottom())),
43 cell_size_.height()));
45 for (int y = min.y(); y <= max.y(); y += cell_size_.height()) {
46 for (int x = min.x(); x <= max.x(); x += cell_size_.width()) {
47 PixelRefMapKey key(x, y);
48 data_hash_map_[key].push_back(it->pixel_ref);
52 min_x = std::min(min_x, min.x());
53 min_y = std::min(min_y, min.y());
54 max_x = std::max(max_x, max.x());
55 max_y = std::max(max_y, max.y());
58 min_pixel_cell_ = gfx::Point(min_x, min_y);
59 max_pixel_cell_ = gfx::Point(max_x, max_y);
62 base::LazyInstance<PixelRefs> PixelRefMap::Iterator::empty_pixel_refs_;
64 PixelRefMap::Iterator::Iterator()
65 : target_pixel_ref_map_(NULL),
66 current_pixel_refs_(empty_pixel_refs_.Pointer()),
67 current_index_(0),
68 min_point_(-1, -1),
69 max_point_(-1, -1),
70 current_x_(0),
71 current_y_(0) {
74 PixelRefMap::Iterator::Iterator(const gfx::Rect& rect, const Picture* picture)
75 : target_pixel_ref_map_(&(picture->pixel_refs_)),
76 current_pixel_refs_(empty_pixel_refs_.Pointer()),
77 current_index_(0) {
78 gfx::Rect layer_rect = picture->layer_rect_;
79 gfx::Size cell_size = target_pixel_ref_map_->cell_size_;
80 DCHECK(!cell_size.IsEmpty());
81 gfx::Rect query_rect(rect);
82 // Early out if the query rect doesn't intersect this picture.
83 if (!query_rect.Intersects(layer_rect)) {
84 min_point_ = gfx::Point(0, 0);
85 max_point_ = gfx::Point(0, 0);
86 current_x_ = 1;
87 current_y_ = 1;
88 return;
91 // First, subtract the layer origin as cells are stored in layer space.
92 query_rect.Offset(-layer_rect.OffsetFromOrigin());
94 // We have to find a cell_size aligned point that corresponds to
95 // query_rect. Point is a multiple of cell_size.
96 min_point_ = gfx::Point(RoundDown(query_rect.x(), cell_size.width()),
97 RoundDown(query_rect.y(), cell_size.height()));
98 max_point_ =
99 gfx::Point(RoundDown(query_rect.right() - 1, cell_size.width()),
100 RoundDown(query_rect.bottom() - 1, cell_size.height()));
102 // Limit the points to known pixel ref boundaries.
103 min_point_ = gfx::Point(
104 std::max(min_point_.x(), target_pixel_ref_map_->min_pixel_cell_.x()),
105 std::max(min_point_.y(), target_pixel_ref_map_->min_pixel_cell_.y()));
106 max_point_ = gfx::Point(
107 std::min(max_point_.x(), target_pixel_ref_map_->max_pixel_cell_.x()),
108 std::min(max_point_.y(), target_pixel_ref_map_->max_pixel_cell_.y()));
110 // Make the current x be cell_size.width() less than min point, so that
111 // the first increment will point at min_point_.
112 current_x_ = min_point_.x() - cell_size.width();
113 current_y_ = min_point_.y();
114 if (current_y_ <= max_point_.y())
115 ++(*this);
118 PixelRefMap::Iterator::~Iterator() {
121 PixelRefMap::Iterator& PixelRefMap::Iterator::operator++() {
122 ++current_index_;
123 // If we're not at the end of the list, then we have the next item.
124 if (current_index_ < current_pixel_refs_->size())
125 return *this;
127 DCHECK(current_y_ <= max_point_.y());
128 while (true) {
129 gfx::Size cell_size = target_pixel_ref_map_->cell_size_;
131 // Advance the current grid cell.
132 current_x_ += cell_size.width();
133 if (current_x_ > max_point_.x()) {
134 current_y_ += cell_size.height();
135 current_x_ = min_point_.x();
136 if (current_y_ > max_point_.y()) {
137 current_pixel_refs_ = empty_pixel_refs_.Pointer();
138 current_index_ = 0;
139 break;
143 // If there are no pixel refs at this grid cell, keep incrementing.
144 PixelRefMapKey key(current_x_, current_y_);
145 PixelRefHashmap::const_iterator iter =
146 target_pixel_ref_map_->data_hash_map_.find(key);
147 if (iter == target_pixel_ref_map_->data_hash_map_.end())
148 continue;
150 // We found a non-empty list: store it and get the first pixel ref.
151 current_pixel_refs_ = &iter->second;
152 current_index_ = 0;
153 break;
155 return *this;
158 } // namespace cc