Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(467)

Side by Side Diff: cc/resources/pixel_ref_map.cc

Issue 909353002: Move PixelRefMap and its Iterator out from Picture class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: no longer override hashmap and pixelrefmap::iterator Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "cc/resources/pixel_ref_map.h"
6
7 #include <algorithm>
8 #include <limits>
9
10 #include "cc/base/util.h"
11 #include "cc/resources/picture.h"
12 #include "skia/ext/pixel_ref_utils.h"
13
14 namespace cc {
15
16 PixelRefMap::PixelRefMap(const gfx::Size& cell_size) : cell_size_(cell_size) {
17 DCHECK(!cell_size.IsEmpty());
18 }
19
20 PixelRefMap::~PixelRefMap() {
21 }
22
23 void PixelRefMap::GatherPixelRefsFromPicture(SkPicture* picture) {
24 DCHECK(picture);
25
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;
30
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()));
44
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 map_[key].push_back(it->pixel_ref);
49 }
50 }
51
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());
56 }
57
58 min_pixel_cell_ = gfx::Point(min_x, min_y);
59 max_pixel_cell_ = gfx::Point(max_x, max_y);
60 }
61
62 base::LazyInstance<PixelRefs> PixelRefMap::Iterator::empty_pixel_refs_;
63
64 PixelRefMap::Iterator::Iterator()
65 : 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) {
72 }
73
74 PixelRefMap::Iterator::Iterator(const gfx::Rect& rect, const Picture* picture)
75 : 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 = 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;
89 }
90
91 // First, subtract the layer origin as cells are stored in layer space.
92 query_rect.Offset(-layer_rect.OffsetFromOrigin());
93
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()));
101
102 // Limit the points to known pixel ref boundaries.
103 min_point_ = gfx::Point(std::max(min_point_.x(), map_->min_pixel_cell_.x()),
104 std::max(min_point_.y(), map_->min_pixel_cell_.y()));
105 max_point_ = gfx::Point(std::min(max_point_.x(), map_->max_pixel_cell_.x()),
106 std::min(max_point_.y(), map_->max_pixel_cell_.y()));
107
108 // Make the current x be cell_size.width() less than min point, so that
109 // the first increment will point at min_point_.
110 current_x_ = min_point_.x() - cell_size.width();
111 current_y_ = min_point_.y();
112 if (current_y_ <= max_point_.y())
113 ++(*this);
114 }
115
116 PixelRefMap::Iterator::~Iterator() {
117 }
118
119 PixelRefMap::Iterator& PixelRefMap::Iterator::operator++() {
120 ++current_index_;
121 // If we're not at the end of the list, then we have the next item.
122 if (current_index_ < current_pixel_refs_->size())
123 return *this;
124
125 DCHECK(current_y_ <= max_point_.y());
126 while (true) {
127 gfx::Size cell_size = map_->cell_size_;
128
129 // Advance the current grid cell.
130 current_x_ += cell_size.width();
131 if (current_x_ > max_point_.x()) {
132 current_y_ += cell_size.height();
133 current_x_ = min_point_.x();
134 if (current_y_ > max_point_.y()) {
135 current_pixel_refs_ = empty_pixel_refs_.Pointer();
136 current_index_ = 0;
137 break;
138 }
139 }
140
141 // If there are no pixel refs at this grid cell, keep incrementing.
142 PixelRefMapKey key(current_x_, current_y_);
143 PixelRefHashmap::const_iterator iter = map_->map_.find(key);
144 if (iter == map_->map_.end())
145 continue;
146
147 // We found a non-empty list: store it and get the first pixel ref.
148 current_pixel_refs_ = &iter->second;
149 current_index_ = 0;
150 break;
151 }
152 return *this;
153 }
154
155 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698