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"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "cc/playback/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 CreateDiscardableBitmap(gfx::Size(500, 500), &discardable_bitmap
[y
][x
]);
44 content_layer_client
.add_draw_bitmap(
45 discardable_bitmap
[y
][x
], gfx::Point(x
* 512 + 6, y
* 512 + 6),
51 scoped_refptr
<Picture
> picture
=
52 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
53 RecordingSource::RECORD_NORMALLY
);
55 // Default iterator does not have any pixel refs.
57 PixelRefMap::Iterator iterator
;
58 EXPECT_FALSE(iterator
);
61 for (int y
= 0; y
< 4; ++y
) {
62 for (int x
= 0; x
< 4; ++x
) {
63 PixelRefMap::Iterator
iterator(gfx::Rect(x
* 512, y
* 512, 500, 500),
66 EXPECT_TRUE(iterator
) << x
<< " " << y
;
67 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[y
][x
].pixelRef())
69 EXPECT_EQ(gfx::RectF(x
* 512 + 6, y
* 512 + 6, 500, 500).ToString(),
70 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
71 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
73 EXPECT_FALSE(iterator
) << x
<< " " << y
;
77 // Capture 4 pixel refs.
79 PixelRefMap::Iterator
iterator(gfx::Rect(512, 512, 2048, 2048),
81 EXPECT_TRUE(iterator
);
82 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][2].pixelRef());
83 EXPECT_EQ(gfx::RectF(2 * 512 + 6, 512 + 6, 500, 500).ToString(),
84 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
85 EXPECT_TRUE(++iterator
);
86 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
87 EXPECT_EQ(gfx::RectF(512 + 6, 2 * 512 + 6, 500, 500).ToString(),
88 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
89 EXPECT_TRUE(++iterator
);
90 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
91 EXPECT_EQ(gfx::RectF(3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
92 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
93 EXPECT_TRUE(++iterator
);
94 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
95 EXPECT_EQ(gfx::RectF(2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
96 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
97 EXPECT_FALSE(++iterator
);
102 PixelRefMap::Iterator
iterator(gfx::Rect(512, 512, 2048, 2048),
104 EXPECT_TRUE(iterator
);
105 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][2].pixelRef());
106 EXPECT_EQ(gfx::RectF(2 * 512 + 6, 512 + 6, 500, 500).ToString(),
107 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
108 EXPECT_TRUE(++iterator
);
109 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
110 EXPECT_EQ(gfx::RectF(512 + 6, 2 * 512 + 6, 500, 500).ToString(),
111 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
113 // copy now points to the same spot as iterator,
114 // but both can be incremented independently.
115 PixelRefMap::Iterator copy
= iterator
;
116 EXPECT_TRUE(++iterator
);
117 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
118 EXPECT_EQ(gfx::RectF(3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
119 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
120 EXPECT_TRUE(++iterator
);
121 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
122 EXPECT_EQ(gfx::RectF(2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
123 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
124 EXPECT_FALSE(++iterator
);
127 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
128 EXPECT_EQ(gfx::RectF(512 + 6, 2 * 512 + 6, 500, 500).ToString(),
129 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
131 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
132 EXPECT_EQ(gfx::RectF(3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
133 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
135 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
136 EXPECT_EQ(gfx::RectF(2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
137 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
138 EXPECT_FALSE(++copy
);
142 TEST(PixelRefMapTest
, PixelRefMapIteratorNonZeroLayer
) {
143 gfx::Rect
layer_rect(1024, 0, 2048, 2048);
145 gfx::Size
tile_grid_size(512, 512);
147 FakeContentLayerClient content_layer_client
;
149 // Discardable pixel refs are found in the following grids:
159 SkBitmap discardable_bitmap
[4][4];
160 for (int y
= 0; y
< 4; ++y
) {
161 for (int x
= 0; x
< 4; ++x
) {
163 CreateDiscardableBitmap(gfx::Size(500, 500), &discardable_bitmap
[y
][x
]);
165 content_layer_client
.add_draw_bitmap(
166 discardable_bitmap
[y
][x
],
167 gfx::Point(1024 + x
* 512 + 6, y
* 512 + 6), paint
);
172 scoped_refptr
<Picture
> picture
=
173 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
174 RecordingSource::RECORD_NORMALLY
);
176 // Default iterator does not have any pixel refs.
178 PixelRefMap::Iterator iterator
;
179 EXPECT_FALSE(iterator
);
182 for (int y
= 0; y
< 4; ++y
) {
183 for (int x
= 0; x
< 4; ++x
) {
184 PixelRefMap::Iterator
iterator(
185 gfx::Rect(1024 + x
* 512, y
* 512, 500, 500), picture
.get());
187 EXPECT_TRUE(iterator
) << x
<< " " << y
;
188 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[y
][x
].pixelRef());
190 gfx::RectF(1024 + x
* 512 + 6, y
* 512 + 6, 500, 500).ToString(),
191 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
192 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
194 EXPECT_FALSE(iterator
) << x
<< " " << y
;
198 // Capture 4 pixel refs.
200 PixelRefMap::Iterator
iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
202 EXPECT_TRUE(iterator
);
203 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][2].pixelRef());
204 EXPECT_EQ(gfx::RectF(1024 + 2 * 512 + 6, 512 + 6, 500, 500).ToString(),
205 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
206 EXPECT_TRUE(++iterator
);
207 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
208 EXPECT_EQ(gfx::RectF(1024 + 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
209 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
210 EXPECT_TRUE(++iterator
);
211 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
212 EXPECT_EQ(gfx::RectF(1024 + 3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
213 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
214 EXPECT_TRUE(++iterator
);
215 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
216 EXPECT_EQ(gfx::RectF(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
217 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
218 EXPECT_FALSE(++iterator
);
223 PixelRefMap::Iterator
iterator(gfx::Rect(1024 + 512, 512, 2048, 2048),
225 EXPECT_TRUE(iterator
);
226 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[1][2].pixelRef());
227 EXPECT_EQ(gfx::RectF(1024 + 2 * 512 + 6, 512 + 6, 500, 500).ToString(),
228 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
229 EXPECT_TRUE(++iterator
);
230 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
231 EXPECT_EQ(gfx::RectF(1024 + 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
232 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
234 // copy now points to the same spot as iterator,
235 // but both can be incremented independently.
236 PixelRefMap::Iterator copy
= iterator
;
237 EXPECT_TRUE(++iterator
);
238 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
239 EXPECT_EQ(gfx::RectF(1024 + 3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
240 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
241 EXPECT_TRUE(++iterator
);
242 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
243 EXPECT_EQ(gfx::RectF(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
244 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
245 EXPECT_FALSE(++iterator
);
248 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[2][1].pixelRef());
249 EXPECT_EQ(gfx::RectF(1024 + 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
250 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
252 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[2][3].pixelRef());
253 EXPECT_EQ(gfx::RectF(1024 + 3 * 512 + 6, 2 * 512 + 6, 500, 500).ToString(),
254 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
256 EXPECT_TRUE(copy
->pixel_ref
== discardable_bitmap
[3][2].pixelRef());
257 EXPECT_EQ(gfx::RectF(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500).ToString(),
258 gfx::SkRectToRectF(copy
->pixel_ref_rect
).ToString());
259 EXPECT_FALSE(++copy
);
262 // Non intersecting rects
264 PixelRefMap::Iterator
iterator(gfx::Rect(0, 0, 1000, 1000), picture
.get());
265 EXPECT_FALSE(iterator
);
268 PixelRefMap::Iterator
iterator(gfx::Rect(3500, 0, 1000, 1000),
270 EXPECT_FALSE(iterator
);
273 PixelRefMap::Iterator
iterator(gfx::Rect(0, 1100, 1000, 1000),
275 EXPECT_FALSE(iterator
);
278 PixelRefMap::Iterator
iterator(gfx::Rect(3500, 1100, 1000, 1000),
280 EXPECT_FALSE(iterator
);
284 TEST(PixelRefMapTest
, PixelRefMapIteratorOnePixelQuery
) {
285 gfx::Rect
layer_rect(2048, 2048);
287 gfx::Size
tile_grid_size(512, 512);
289 FakeContentLayerClient content_layer_client
;
291 // Discardable pixel refs are found in the following grids:
301 SkBitmap discardable_bitmap
[4][4];
302 for (int y
= 0; y
< 4; ++y
) {
303 for (int x
= 0; x
< 4; ++x
) {
305 CreateDiscardableBitmap(gfx::Size(500, 500), &discardable_bitmap
[y
][x
]);
307 content_layer_client
.add_draw_bitmap(
308 discardable_bitmap
[y
][x
], gfx::Point(x
* 512 + 6, y
* 512 + 6),
314 scoped_refptr
<Picture
> picture
=
315 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
316 RecordingSource::RECORD_NORMALLY
);
318 // Default iterator does not have any pixel refs.
320 PixelRefMap::Iterator iterator
;
321 EXPECT_FALSE(iterator
);
324 for (int y
= 0; y
< 4; ++y
) {
325 for (int x
= 0; x
< 4; ++x
) {
326 PixelRefMap::Iterator
iterator(gfx::Rect(x
* 512, y
* 512 + 256, 1, 1),
329 EXPECT_TRUE(iterator
) << x
<< " " << y
;
330 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
[y
][x
].pixelRef());
331 EXPECT_EQ(gfx::RectF(x
* 512 + 6, y
* 512 + 6, 500, 500).ToString(),
332 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
333 EXPECT_FALSE(++iterator
) << x
<< " " << y
;
335 EXPECT_FALSE(iterator
) << x
<< " " << y
;
341 TEST(PixelRefMapTest
, PixelRefMapIteratorMassiveImage
) {
342 gfx::Rect
layer_rect(2048, 2048);
343 gfx::Size
tile_grid_size(512, 512);
344 FakeContentLayerClient content_layer_client
;
346 SkBitmap discardable_bitmap
;
347 CreateDiscardableBitmap(gfx::Size(1 << 25, 1 << 25), &discardable_bitmap
);
349 content_layer_client
.add_draw_bitmap(discardable_bitmap
, gfx::Point(0, 0),
352 scoped_refptr
<Picture
> picture
=
353 Picture::Create(layer_rect
, &content_layer_client
, tile_grid_size
, true,
354 RecordingSource::RECORD_NORMALLY
);
356 PixelRefMap::Iterator
iterator(gfx::Rect(0, 0, 1, 1), picture
.get());
357 EXPECT_TRUE(iterator
);
358 EXPECT_TRUE(iterator
->pixel_ref
== discardable_bitmap
.pixelRef());
359 EXPECT_EQ(gfx::RectF(0, 0, 1 << 25, 1 << 25).ToString(),
360 gfx::SkRectToRectF(iterator
->pixel_ref_rect
).ToString());
361 EXPECT_FALSE(++iterator
);