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"
10 #include "cc/base/util.h"
11 #include "cc/resources/display_item_list.h"
12 #include "cc/resources/picture.h"
13 #include "skia/ext/pixel_ref_utils.h"
17 PixelRefMap::PixelRefMap(const gfx::Size
& cell_size
) : cell_size_(cell_size
) {
18 DCHECK(!cell_size
.IsEmpty());
21 PixelRefMap::~PixelRefMap() {
24 void PixelRefMap::GatherPixelRefsFromPicture(SkPicture
* picture
) {
27 int min_x
= std::numeric_limits
<int>::max();
28 int min_y
= std::numeric_limits
<int>::max();
32 skia::DiscardablePixelRefList pixel_refs
;
33 skia::PixelRefUtils::GatherDiscardablePixelRefs(picture
, &pixel_refs
);
34 for (skia::DiscardablePixelRefList::const_iterator it
= pixel_refs
.begin();
35 it
!= pixel_refs
.end(); ++it
) {
37 RoundDown(static_cast<int>(it
->pixel_ref_rect
.x()), cell_size_
.width()),
38 RoundDown(static_cast<int>(it
->pixel_ref_rect
.y()),
39 cell_size_
.height()));
41 RoundDown(static_cast<int>(std::ceil(it
->pixel_ref_rect
.right())),
43 RoundDown(static_cast<int>(std::ceil(it
->pixel_ref_rect
.bottom())),
44 cell_size_
.height()));
46 for (int y
= min
.y(); y
<= max
.y(); y
+= cell_size_
.height()) {
47 for (int x
= min
.x(); x
<= max
.x(); x
+= cell_size_
.width()) {
48 PixelRefMapKey
key(x
, y
);
49 data_hash_map_
[key
].push_back(it
->pixel_ref
);
53 min_x
= std::min(min_x
, min
.x());
54 min_y
= std::min(min_y
, min
.y());
55 max_x
= std::max(max_x
, max
.x());
56 max_y
= std::max(max_y
, max
.y());
59 min_pixel_cell_
= gfx::Point(min_x
, min_y
);
60 max_pixel_cell_
= gfx::Point(max_x
, max_y
);
63 base::LazyInstance
<PixelRefs
> PixelRefMap::Iterator::empty_pixel_refs_
;
65 PixelRefMap::Iterator::Iterator()
66 : target_pixel_ref_map_(NULL
),
67 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
75 PixelRefMap::Iterator::Iterator(const gfx::Rect
& rect
, const Picture
* picture
)
76 : target_pixel_ref_map_(&(picture
->pixel_refs_
)),
77 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
79 map_layer_rect_
= picture
->layer_rect_
;
80 PointToFirstPixelRef(rect
);
83 PixelRefMap::Iterator::Iterator(const gfx::Rect
& rect
,
84 const DisplayItemList
* display_list
)
85 : target_pixel_ref_map_(display_list
->pixel_refs_
.get()),
86 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
88 map_layer_rect_
= display_list
->layer_rect_
;
89 PointToFirstPixelRef(rect
);
92 PixelRefMap::Iterator::~Iterator() {
95 PixelRefMap::Iterator
& PixelRefMap::Iterator::operator++() {
97 // If we're not at the end of the list, then we have the next item.
98 if (current_index_
< current_pixel_refs_
->size())
101 DCHECK(current_y_
<= max_point_
.y());
103 gfx::Size cell_size
= target_pixel_ref_map_
->cell_size_
;
105 // Advance the current grid cell.
106 current_x_
+= cell_size
.width();
107 if (current_x_
> max_point_
.x()) {
108 current_y_
+= cell_size
.height();
109 current_x_
= min_point_
.x();
110 if (current_y_
> max_point_
.y()) {
111 current_pixel_refs_
= empty_pixel_refs_
.Pointer();
117 // If there are no pixel refs at this grid cell, keep incrementing.
118 PixelRefMapKey
key(current_x_
, current_y_
);
119 PixelRefHashmap::const_iterator iter
=
120 target_pixel_ref_map_
->data_hash_map_
.find(key
);
121 if (iter
== target_pixel_ref_map_
->data_hash_map_
.end())
124 // We found a non-empty list: store it and get the first pixel ref.
125 current_pixel_refs_
= &iter
->second
;
132 void PixelRefMap::Iterator::PointToFirstPixelRef(const gfx::Rect
& rect
) {
133 gfx::Rect
query_rect(rect
);
134 // Early out if the query rect doesn't intersect this picture.
135 if (!query_rect
.Intersects(map_layer_rect_
) || !target_pixel_ref_map_
) {
136 min_point_
= gfx::Point(0, 0);
137 max_point_
= gfx::Point(0, 0);
143 // First, subtract the layer origin as cells are stored in layer space.
144 query_rect
.Offset(-map_layer_rect_
.OffsetFromOrigin());
146 DCHECK(!target_pixel_ref_map_
->cell_size_
.IsEmpty());
147 gfx::Size
cell_size(target_pixel_ref_map_
->cell_size_
);
148 // We have to find a cell_size aligned point that corresponds to
149 // query_rect. Point is a multiple of cell_size.
150 min_point_
= gfx::Point(RoundDown(query_rect
.x(), cell_size
.width()),
151 RoundDown(query_rect
.y(), cell_size
.height()));
153 gfx::Point(RoundDown(query_rect
.right() - 1, cell_size
.width()),
154 RoundDown(query_rect
.bottom() - 1, cell_size
.height()));
156 // Limit the points to known pixel ref boundaries.
157 min_point_
= gfx::Point(
158 std::max(min_point_
.x(), target_pixel_ref_map_
->min_pixel_cell_
.x()),
159 std::max(min_point_
.y(), target_pixel_ref_map_
->min_pixel_cell_
.y()));
160 max_point_
= gfx::Point(
161 std::min(max_point_
.x(), target_pixel_ref_map_
->max_pixel_cell_
.x()),
162 std::min(max_point_
.y(), target_pixel_ref_map_
->max_pixel_cell_
.y()));
164 // Make the current x be cell_size.width() less than min point, so that
165 // the first increment will point at min_point_.
166 current_x_
= min_point_
.x() - cell_size
.width();
167 current_y_
= min_point_
.y();
168 if (current_y_
<= max_point_
.y())