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/playback/pixel_ref_map.h"
10 #include "cc/base/math_util.h"
11 #include "cc/playback/display_item_list.h"
12 #include "cc/playback/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
) {
36 gfx::Point
min(MathUtil::RoundDown(static_cast<int>(it
->pixel_ref_rect
.x()),
38 MathUtil::RoundDown(static_cast<int>(it
->pixel_ref_rect
.y()),
39 cell_size_
.height()));
40 gfx::Point
max(MathUtil::RoundDown(
41 static_cast<int>(std::ceil(it
->pixel_ref_rect
.right())),
44 static_cast<int>(std::ceil(it
->pixel_ref_rect
.bottom())),
45 cell_size_
.height()));
47 for (int y
= min
.y(); y
<= max
.y(); y
+= cell_size_
.height()) {
48 for (int x
= min
.x(); x
<= max
.x(); x
+= cell_size_
.width()) {
49 PixelRefMapKey
key(x
, y
);
50 data_hash_map_
[key
].push_back(it
->pixel_ref
);
54 min_x
= std::min(min_x
, min
.x());
55 min_y
= std::min(min_y
, min
.y());
56 max_x
= std::max(max_x
, max
.x());
57 max_y
= std::max(max_y
, max
.y());
60 min_pixel_cell_
= gfx::Point(min_x
, min_y
);
61 max_pixel_cell_
= gfx::Point(max_x
, max_y
);
64 base::LazyInstance
<PixelRefs
> PixelRefMap::Iterator::empty_pixel_refs_
;
66 PixelRefMap::Iterator::Iterator()
67 : target_pixel_ref_map_(NULL
),
68 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
76 PixelRefMap::Iterator::Iterator(const gfx::Rect
& rect
, const Picture
* picture
)
77 : target_pixel_ref_map_(&(picture
->pixel_refs_
)),
78 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
80 map_layer_rect_
= picture
->layer_rect_
;
81 PointToFirstPixelRef(rect
);
84 PixelRefMap::Iterator::Iterator(const gfx::Rect
& rect
,
85 const DisplayItemList
* display_list
)
86 : target_pixel_ref_map_(display_list
->pixel_refs_
.get()),
87 current_pixel_refs_(empty_pixel_refs_
.Pointer()),
89 map_layer_rect_
= display_list
->layer_rect_
;
90 PointToFirstPixelRef(rect
);
93 PixelRefMap::Iterator::~Iterator() {
96 PixelRefMap::Iterator
& PixelRefMap::Iterator::operator++() {
98 // If we're not at the end of the list, then we have the next item.
99 if (current_index_
< current_pixel_refs_
->size())
102 DCHECK(current_y_
<= max_point_
.y());
104 gfx::Size cell_size
= target_pixel_ref_map_
->cell_size_
;
106 // Advance the current grid cell.
107 current_x_
+= cell_size
.width();
108 if (current_x_
> max_point_
.x()) {
109 current_y_
+= cell_size
.height();
110 current_x_
= min_point_
.x();
111 if (current_y_
> max_point_
.y()) {
112 current_pixel_refs_
= empty_pixel_refs_
.Pointer();
118 // If there are no pixel refs at this grid cell, keep incrementing.
119 PixelRefMapKey
key(current_x_
, current_y_
);
120 PixelRefHashmap::const_iterator iter
=
121 target_pixel_ref_map_
->data_hash_map_
.find(key
);
122 if (iter
== target_pixel_ref_map_
->data_hash_map_
.end())
125 // We found a non-empty list: store it and get the first pixel ref.
126 current_pixel_refs_
= &iter
->second
;
133 void PixelRefMap::Iterator::PointToFirstPixelRef(const gfx::Rect
& rect
) {
134 gfx::Rect
query_rect(rect
);
135 // Early out if the query rect doesn't intersect this picture.
136 if (!query_rect
.Intersects(map_layer_rect_
) || !target_pixel_ref_map_
) {
137 min_point_
= gfx::Point(0, 0);
138 max_point_
= gfx::Point(0, 0);
144 // First, subtract the layer origin as cells are stored in layer space.
145 query_rect
.Offset(-map_layer_rect_
.OffsetFromOrigin());
147 DCHECK(!target_pixel_ref_map_
->cell_size_
.IsEmpty());
148 gfx::Size
cell_size(target_pixel_ref_map_
->cell_size_
);
149 // We have to find a cell_size aligned point that corresponds to
150 // query_rect. Point is a multiple of cell_size.
152 gfx::Point(MathUtil::RoundDown(query_rect
.x(), cell_size
.width()),
153 MathUtil::RoundDown(query_rect
.y(), cell_size
.height()));
154 max_point_
= gfx::Point(
155 MathUtil::RoundDown(query_rect
.right() - 1, cell_size
.width()),
156 MathUtil::RoundDown(query_rect
.bottom() - 1, cell_size
.height()));
158 // Limit the points to known pixel ref boundaries.
159 min_point_
= gfx::Point(
160 std::max(min_point_
.x(), target_pixel_ref_map_
->min_pixel_cell_
.x()),
161 std::max(min_point_
.y(), target_pixel_ref_map_
->min_pixel_cell_
.y()));
162 max_point_
= gfx::Point(
163 std::min(max_point_
.x(), target_pixel_ref_map_
->max_pixel_cell_
.x()),
164 std::min(max_point_
.y(), target_pixel_ref_map_
->max_pixel_cell_
.y()));
166 // Make the current x be cell_size.width() less than min point, so that
167 // the first increment will point at min_point_.
168 current_x_
= min_point_
.x() - cell_size
.width();
169 current_y_
= min_point_
.y();
170 if (current_y_
<= max_point_
.y())