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 "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "cc/resources/picture.h"
11 #include "cc/test/fake_content_layer_client.h"
12 #include "cc/test/skia_common.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkGraphics.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/skia_util.h"
21 TEST(PixelRefMapTest
, PixelRefMapIterator
) {
22 gfx::Rect
layer_rect(2048, 2048);
24 gfx::Size
tile_grid_size(512, 512);
26 FakeContentLayerClient content_layer_client
;
28 // Discardable pixel refs are found in the following grids:
38 SkBitmap discardable_bitmap
[4][4];
39 for (int y
= 0; y
< 4; ++y
) {
40 for (int x
= 0; x
< 4; ++x
) {
42 CreateBitmap(gfx::Size(500, 500), "discardable",
43 &discardable_bitmap
[y
][x
]);
45 content_layer_client
.add_draw_bitmap(
46 discardable_bitmap
[y
][x
], gfx::Point(x
* 512 + 6, y
* 512 + 6),
52 scoped_refptr
<Picture
> picture
=
53 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
54 RecordingSource::RECORD_NORMALLY
);
56 // Default iterator does not have any pixel refs.
58 PixelRefMap::Iterator iterator
;
59 EXPECT_FALSE(iterator
);
62 for (int y
= 0; y
< 4; ++y
) {
63 for (int x
= 0; x
< 4; ++x
) {
64 PixelRefMap::Iterator
iterator(gfx::Rect(x
* 512, y
* 512, 500, 500),
67 EXPECT_TRUE(iterator
) << x
<< " " << y
;
68 EXPECT_TRUE(*iterator
== discardable_bitmap
[y
][x
].pixelRef())
70 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
72 EXPECT_FALSE(iterator
) << x
<< " " << y
;
76 // Capture 4 pixel refs.
78 PixelRefMap::Iterator
iterator(gfx::Rect(512, 512, 2048, 2048),
80 EXPECT_TRUE(iterator
);
81 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][2].pixelRef());
82 EXPECT_TRUE(++iterator
);
83 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][1].pixelRef());
84 EXPECT_TRUE(++iterator
);
85 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][3].pixelRef());
86 EXPECT_TRUE(++iterator
);
87 EXPECT_TRUE(*iterator
== discardable_bitmap
[3][2].pixelRef());
88 EXPECT_FALSE(++iterator
);
92 PixelRefMap::Iterator
iterator(gfx::Rect(512, 512, 2048, 2048),
94 EXPECT_TRUE(iterator
);
95 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][2].pixelRef());
96 EXPECT_TRUE(++iterator
);
97 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][1].pixelRef());
99 // copy now points to the same spot as iterator,
100 // but both can be incremented independently.
101 PixelRefMap::Iterator copy
= iterator
;
102 EXPECT_TRUE(++iterator
);
103 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][3].pixelRef());
104 EXPECT_TRUE(++iterator
);
105 EXPECT_TRUE(*iterator
== discardable_bitmap
[3][2].pixelRef());
106 EXPECT_FALSE(++iterator
);
109 EXPECT_TRUE(*copy
== discardable_bitmap
[2][1].pixelRef());
111 EXPECT_TRUE(*copy
== discardable_bitmap
[2][3].pixelRef());
113 EXPECT_TRUE(*copy
== discardable_bitmap
[3][2].pixelRef());
114 EXPECT_FALSE(++copy
);
117 TEST(PixelRefMapTest
, PixelRefMapIteratorNonZeroLayer
) {
118 gfx::Rect
layer_rect(1024, 0, 2048, 2048);
120 gfx::Size
tile_grid_size(512, 512);
122 FakeContentLayerClient content_layer_client
;
124 // Discardable pixel refs are found in the following grids:
134 SkBitmap discardable_bitmap
[4][4];
135 for (int y
= 0; y
< 4; ++y
) {
136 for (int x
= 0; x
< 4; ++x
) {
138 CreateBitmap(gfx::Size(500, 500), "discardable",
139 &discardable_bitmap
[y
][x
]);
141 content_layer_client
.add_draw_bitmap(
142 discardable_bitmap
[y
][x
],
143 gfx::Point(1024 + x
* 512 + 6, y
* 512 + 6), paint
);
148 scoped_refptr
<Picture
> picture
=
149 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
150 RecordingSource::RECORD_NORMALLY
);
152 // Default iterator does not have any pixel refs.
154 PixelRefMap::Iterator iterator
;
155 EXPECT_FALSE(iterator
);
158 for (int y
= 0; y
< 4; ++y
) {
159 for (int x
= 0; x
< 4; ++x
) {
160 PixelRefMap::Iterator
iterator(
161 gfx::Rect(1024 + x
* 512, y
* 512, 500, 500), picture
.get());
163 EXPECT_TRUE(iterator
) << x
<< " " << y
;
164 EXPECT_TRUE(*iterator
== discardable_bitmap
[y
][x
].pixelRef());
165 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
167 EXPECT_FALSE(iterator
) << x
<< " " << y
;
171 // Capture 4 pixel refs.
173 PixelRefMap::Iterator
iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
175 EXPECT_TRUE(iterator
);
176 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][2].pixelRef());
177 EXPECT_TRUE(++iterator
);
178 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][1].pixelRef());
179 EXPECT_TRUE(++iterator
);
180 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][3].pixelRef());
181 EXPECT_TRUE(++iterator
);
182 EXPECT_TRUE(*iterator
== discardable_bitmap
[3][2].pixelRef());
183 EXPECT_FALSE(++iterator
);
188 PixelRefMap::Iterator
iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
190 EXPECT_TRUE(iterator
);
191 EXPECT_TRUE(*iterator
== discardable_bitmap
[1][2].pixelRef());
192 EXPECT_TRUE(++iterator
);
193 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][1].pixelRef());
195 // copy now points to the same spot as iterator,
196 // but both can be incremented independently.
197 PixelRefMap::Iterator copy
= iterator
;
198 EXPECT_TRUE(++iterator
);
199 EXPECT_TRUE(*iterator
== discardable_bitmap
[2][3].pixelRef());
200 EXPECT_TRUE(++iterator
);
201 EXPECT_TRUE(*iterator
== discardable_bitmap
[3][2].pixelRef());
202 EXPECT_FALSE(++iterator
);
205 EXPECT_TRUE(*copy
== discardable_bitmap
[2][1].pixelRef());
207 EXPECT_TRUE(*copy
== discardable_bitmap
[2][3].pixelRef());
209 EXPECT_TRUE(*copy
== discardable_bitmap
[3][2].pixelRef());
210 EXPECT_FALSE(++copy
);
213 // Non intersecting rects
215 PixelRefMap::Iterator
iterator(gfx::Rect(0, 0, 1000, 1000), picture
.get());
216 EXPECT_FALSE(iterator
);
219 PixelRefMap::Iterator
iterator(gfx::Rect(3500, 0, 1000, 1000),
221 EXPECT_FALSE(iterator
);
224 PixelRefMap::Iterator
iterator(gfx::Rect(0, 1100, 1000, 1000),
226 EXPECT_FALSE(iterator
);
229 PixelRefMap::Iterator
iterator(gfx::Rect(3500, 1100, 1000, 1000),
231 EXPECT_FALSE(iterator
);
235 TEST(PixelRefMapTest
, PixelRefMapIteratorOnePixelQuery
) {
236 gfx::Rect
layer_rect(2048, 2048);
238 gfx::Size
tile_grid_size(512, 512);
240 FakeContentLayerClient content_layer_client
;
242 // Discardable pixel refs are found in the following grids:
252 SkBitmap discardable_bitmap
[4][4];
253 for (int y
= 0; y
< 4; ++y
) {
254 for (int x
= 0; x
< 4; ++x
) {
256 CreateBitmap(gfx::Size(500, 500), "discardable",
257 &discardable_bitmap
[y
][x
]);
259 content_layer_client
.add_draw_bitmap(
260 discardable_bitmap
[y
][x
], gfx::Point(x
* 512 + 6, y
* 512 + 6),
266 scoped_refptr
<Picture
> picture
=
267 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
268 RecordingSource::RECORD_NORMALLY
);
270 // Default iterator does not have any pixel refs.
272 PixelRefMap::Iterator iterator
;
273 EXPECT_FALSE(iterator
);
276 for (int y
= 0; y
< 4; ++y
) {
277 for (int x
= 0; x
< 4; ++x
) {
278 PixelRefMap::Iterator
iterator(gfx::Rect(x
* 512, y
* 512 + 256, 1, 1),
281 EXPECT_TRUE(iterator
) << x
<< " " << y
;
282 EXPECT_TRUE(*iterator
== discardable_bitmap
[y
][x
].pixelRef());
283 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
285 EXPECT_FALSE(iterator
) << x
<< " " << y
;