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

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: address review comments Created 5 years, 10 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 (*this)[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> PixelRefMapIterator::empty_pixel_refs_;
63
64 PixelRefMapIterator::PixelRefMapIterator()
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 PixelRefMapIterator::PixelRefMapIterator(const gfx::Rect& rect,
75 const Picture* picture)
76 : map_(&(picture->pixel_refs_)),
77 current_pixel_refs_(empty_pixel_refs_.Pointer()),
78 current_index_(0) {
79 gfx::Rect layer_rect = picture->layer_rect_;
80 gfx::Size cell_size = map_->cell_size_;
81 DCHECK(!cell_size.IsEmpty());
82 gfx::Rect query_rect(rect);
83 // Early out if the query rect doesn't intersect this picture.
84 if (!query_rect.Intersects(layer_rect)) {
85 min_point_ = gfx::Point(0, 0);
86 max_point_ = gfx::Point(0, 0);
87 current_x_ = 1;
88 current_y_ = 1;
89 return;
90 }
91
92 // First, subtract the layer origin as cells are stored in layer space.
93 query_rect.Offset(-layer_rect.OffsetFromOrigin());
94
95 // We have to find a cell_size aligned point that corresponds to
96 // query_rect. Point is a multiple of cell_size.
97 min_point_ = gfx::Point(RoundDown(query_rect.x(), cell_size.width()),
98 RoundDown(query_rect.y(), cell_size.height()));
99 max_point_ =
100 gfx::Point(RoundDown(query_rect.right() - 1, cell_size.width()),
101 RoundDown(query_rect.bottom() - 1, cell_size.height()));
102
103 // Limit the points to known pixel ref boundaries.
104 min_point_ = gfx::Point(std::max(min_point_.x(), map_->min_pixel_cell_.x()),
105 std::max(min_point_.y(), map_->min_pixel_cell_.y()));
106 max_point_ = gfx::Point(std::min(max_point_.x(), map_->max_pixel_cell_.x()),
107 std::min(max_point_.y(), map_->max_pixel_cell_.y()));
108
109 // Make the current x be cell_size.width() less than min point, so that
110 // the first increment will point at min_point_.
111 current_x_ = min_point_.x() - cell_size.width();
112 current_y_ = min_point_.y();
113 if (current_y_ <= max_point_.y())
114 ++(*this);
115 }
116
117 PixelRefMapIterator::~PixelRefMapIterator() {
118 }
119
120 PixelRefMapIterator& PixelRefMapIterator::operator++() {
121 ++current_index_;
122 // If we're not at the end of the list, then we have the next item.
123 if (current_index_ < current_pixel_refs_->size())
124 return *this;
125
126 DCHECK(current_y_ <= max_point_.y());
127 while (true) {
128 gfx::Size cell_size = map_->cell_size_;
129
130 // Advance the current grid cell.
131 current_x_ += cell_size.width();
132 if (current_x_ > max_point_.x()) {
133 current_y_ += cell_size.height();
134 current_x_ = min_point_.x();
135 if (current_y_ > max_point_.y()) {
136 current_pixel_refs_ = empty_pixel_refs_.Pointer();
137 current_index_ = 0;
138 break;
139 }
140 }
141
142 // If there are no pixel refs at this grid cell, keep incrementing.
143 PixelRefMapKey key(current_x_, current_y_);
144 PixelRefMap::const_iterator iter = map_->find(key);
145 if (iter == map_->end())
146 continue;
147
148 // We found a non-empty list: store it and get the first pixel ref.
149 current_pixel_refs_ = &iter->second;
150 current_index_ = 0;
151 break;
152 }
153 return *this;
154 }
155
156 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698