Re-land: C++ readability review
[chromium-blink-merge.git] / cc / resources / recording_source_unittest.cc
blob1e0bd42b677518f0976f1f2eb95acc1a1555b15c
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 <vector>
7 #include "cc/resources/display_list_raster_source.h"
8 #include "cc/test/fake_display_list_recording_source.h"
9 #include "cc/test/fake_picture_pile.h"
10 #include "cc/test/fake_picture_pile_impl.h"
11 #include "cc/test/impl_side_painting_settings.h"
12 #include "cc/test/skia_common.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace cc {
16 namespace {
18 template <class T>
19 scoped_ptr<T> CreateRecordingSource(const gfx::Rect& viewport,
20 const gfx::Size& grid_cell_size);
22 template <>
23 scoped_ptr<FakePicturePile> CreateRecordingSource<FakePicturePile>(
24 const gfx::Rect& viewport,
25 const gfx::Size& grid_cell_size) {
26 return FakePicturePile::CreateFilledPile(grid_cell_size, viewport.size());
29 template <>
30 scoped_ptr<FakeDisplayListRecordingSource> CreateRecordingSource<
31 FakeDisplayListRecordingSource>(const gfx::Rect& viewport,
32 const gfx::Size& grid_cell_size) {
33 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
34 FakeDisplayListRecordingSource::CreateRecordingSource(viewport);
35 recording_source->SetGridCellSize(grid_cell_size);
37 return recording_source.Pass();
40 template <class T>
41 scoped_refptr<RasterSource> CreateRasterSource(T* recording_source);
43 template <>
44 scoped_refptr<RasterSource> CreateRasterSource(
45 FakePicturePile* recording_source) {
46 return FakePicturePileImpl::CreateFromPile(recording_source, nullptr);
49 template <>
50 scoped_refptr<RasterSource> CreateRasterSource(
51 FakeDisplayListRecordingSource* recording_source) {
52 bool can_use_lcd_text = true;
53 return DisplayListRasterSource::CreateFromDisplayListRecordingSource(
54 recording_source, can_use_lcd_text);
57 template <typename T>
58 class RecordingSourceTest : public testing::Test {};
60 using testing::Types;
62 typedef Types<FakePicturePile, FakeDisplayListRecordingSource>
63 RecordingSourceImplementations;
65 TYPED_TEST_CASE(RecordingSourceTest, RecordingSourceImplementations);
67 TYPED_TEST(RecordingSourceTest, NoGatherPixelRefEmptyPixelRefs) {
68 gfx::Size grid_cell_size(128, 128);
69 gfx::Rect recorded_viewport(0, 0, 256, 256);
71 scoped_ptr<TypeParam> recording_source =
72 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
73 recording_source->SetGatherPixelRefs(false);
74 recording_source->Rerecord();
76 scoped_refptr<RasterSource> raster_source =
77 CreateRasterSource<TypeParam>(recording_source.get());
79 // If recording source do not gather pixel ref, raster source is not going to
80 // get pixel refs.
82 std::vector<SkPixelRef*> pixel_refs;
83 raster_source->GatherPixelRefs(recorded_viewport, 1.0, &pixel_refs);
84 EXPECT_TRUE(pixel_refs.empty());
88 TYPED_TEST(RecordingSourceTest, EmptyPixelRefs) {
89 gfx::Size grid_cell_size(128, 128);
90 gfx::Rect recorded_viewport(0, 0, 256, 256);
92 scoped_ptr<TypeParam> recording_source =
93 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
94 recording_source->SetGatherPixelRefs(true);
95 recording_source->Rerecord();
97 scoped_refptr<RasterSource> raster_source =
98 CreateRasterSource<TypeParam>(recording_source.get());
100 // Tile sized iterators.
102 std::vector<SkPixelRef*> pixel_refs;
103 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 1.0, &pixel_refs);
104 EXPECT_TRUE(pixel_refs.empty());
107 std::vector<SkPixelRef*> pixel_refs;
108 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 2.0, &pixel_refs);
109 EXPECT_TRUE(pixel_refs.empty());
112 std::vector<SkPixelRef*> pixel_refs;
113 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 64, 64), 0.5, &pixel_refs);
114 EXPECT_TRUE(pixel_refs.empty());
116 // Shifted tile sized iterators.
118 std::vector<SkPixelRef*> pixel_refs;
119 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), 1.0,
120 &pixel_refs);
121 EXPECT_TRUE(pixel_refs.empty());
124 std::vector<SkPixelRef*> pixel_refs;
125 raster_source->GatherPixelRefs(gfx::Rect(280, 280, 256, 256), 2.0,
126 &pixel_refs);
127 EXPECT_TRUE(pixel_refs.empty());
130 std::vector<SkPixelRef*> pixel_refs;
131 raster_source->GatherPixelRefs(gfx::Rect(70, 70, 64, 64), 0.5, &pixel_refs);
132 EXPECT_TRUE(pixel_refs.empty());
134 // Layer sized iterators.
136 std::vector<SkPixelRef*> pixel_refs;
137 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 1.0, &pixel_refs);
138 EXPECT_TRUE(pixel_refs.empty());
141 std::vector<SkPixelRef*> pixel_refs;
142 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
143 EXPECT_TRUE(pixel_refs.empty());
146 std::vector<SkPixelRef*> pixel_refs;
147 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
148 EXPECT_TRUE(pixel_refs.empty());
152 TYPED_TEST(RecordingSourceTest, NoDiscardablePixelRefs) {
153 gfx::Size grid_cell_size(128, 128);
154 gfx::Rect recorded_viewport(0, 0, 256, 256);
156 scoped_ptr<TypeParam> recording_source =
157 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
159 SkPaint simple_paint;
160 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
162 SkBitmap non_discardable_bitmap;
163 CreateBitmap(gfx::Size(128, 128), "notdiscardable", &non_discardable_bitmap);
165 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256),
166 simple_paint);
167 recording_source->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512),
168 simple_paint);
169 recording_source->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256),
170 simple_paint);
171 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256),
172 simple_paint);
173 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(128, 0));
174 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 128));
175 recording_source->add_draw_bitmap(non_discardable_bitmap,
176 gfx::Point(150, 150));
177 recording_source->SetGatherPixelRefs(true);
178 recording_source->Rerecord();
180 scoped_refptr<RasterSource> raster_source =
181 CreateRasterSource<TypeParam>(recording_source.get());
183 // Tile sized iterators.
185 std::vector<SkPixelRef*> pixel_refs;
186 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 1.0, &pixel_refs);
187 EXPECT_TRUE(pixel_refs.empty());
190 std::vector<SkPixelRef*> pixel_refs;
191 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 2.0, &pixel_refs);
192 EXPECT_TRUE(pixel_refs.empty());
195 std::vector<SkPixelRef*> pixel_refs;
196 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 64, 64), 0.5, &pixel_refs);
197 EXPECT_TRUE(pixel_refs.empty());
199 // Shifted tile sized iterators.
201 std::vector<SkPixelRef*> pixel_refs;
202 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), 1.0,
203 &pixel_refs);
204 EXPECT_TRUE(pixel_refs.empty());
207 std::vector<SkPixelRef*> pixel_refs;
208 raster_source->GatherPixelRefs(gfx::Rect(280, 280, 256, 256), 2.0,
209 &pixel_refs);
210 EXPECT_TRUE(pixel_refs.empty());
213 std::vector<SkPixelRef*> pixel_refs;
214 raster_source->GatherPixelRefs(gfx::Rect(70, 70, 64, 64), 0.5, &pixel_refs);
215 EXPECT_TRUE(pixel_refs.empty());
217 // Layer sized iterators.
219 std::vector<SkPixelRef*> pixel_refs;
220 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 1.0, &pixel_refs);
221 EXPECT_TRUE(pixel_refs.empty());
224 std::vector<SkPixelRef*> pixel_refs;
225 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
226 EXPECT_TRUE(pixel_refs.empty());
229 std::vector<SkPixelRef*> pixel_refs;
230 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
231 EXPECT_TRUE(pixel_refs.empty());
235 TYPED_TEST(RecordingSourceTest, DiscardablePixelRefs) {
236 gfx::Size grid_cell_size(128, 128);
237 gfx::Rect recorded_viewport(0, 0, 256, 256);
239 scoped_ptr<TypeParam> recording_source =
240 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
242 SkBitmap discardable_bitmap[2][2];
243 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][0]);
244 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][0]);
245 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][1]);
247 // Discardable pixel refs are found in the following cells:
248 // |---|---|
249 // | x | |
250 // |---|---|
251 // | x | x |
252 // |---|---|
253 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
254 recording_source->add_draw_bitmap(discardable_bitmap[1][0],
255 gfx::Point(0, 130));
256 recording_source->add_draw_bitmap(discardable_bitmap[1][1],
257 gfx::Point(140, 140));
258 recording_source->SetGatherPixelRefs(true);
259 recording_source->Rerecord();
261 scoped_refptr<RasterSource> raster_source =
262 CreateRasterSource<TypeParam>(recording_source.get());
264 // Tile sized iterators. These should find only one pixel ref.
266 std::vector<SkPixelRef*> pixel_refs;
267 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 1.0, &pixel_refs);
268 EXPECT_FALSE(pixel_refs.empty());
269 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
270 EXPECT_EQ(1u, pixel_refs.size());
273 std::vector<SkPixelRef*> pixel_refs;
274 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 2.0, &pixel_refs);
275 EXPECT_FALSE(pixel_refs.empty());
276 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
277 EXPECT_EQ(1u, pixel_refs.size());
280 std::vector<SkPixelRef*> pixel_refs;
281 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 64, 64), 0.5, &pixel_refs);
282 EXPECT_FALSE(pixel_refs.empty());
283 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
284 EXPECT_EQ(1u, pixel_refs.size());
287 // Shifted tile sized iterators. These should find only one pixel ref.
289 std::vector<SkPixelRef*> pixel_refs;
290 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), 1.0,
291 &pixel_refs);
292 EXPECT_FALSE(pixel_refs.empty());
293 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
294 EXPECT_EQ(1u, pixel_refs.size());
297 std::vector<SkPixelRef*> pixel_refs;
298 raster_source->GatherPixelRefs(gfx::Rect(280, 280, 256, 256), 2.0,
299 &pixel_refs);
300 EXPECT_FALSE(pixel_refs.empty());
301 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
302 EXPECT_EQ(1u, pixel_refs.size());
305 std::vector<SkPixelRef*> pixel_refs;
306 raster_source->GatherPixelRefs(gfx::Rect(70, 70, 64, 64), 0.5, &pixel_refs);
307 EXPECT_FALSE(pixel_refs.empty());
308 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
309 EXPECT_EQ(1u, pixel_refs.size());
312 // Ensure there's no discardable pixel refs in the empty cell
314 std::vector<SkPixelRef*> pixel_refs;
315 raster_source->GatherPixelRefs(gfx::Rect(140, 0, 128, 128), 1.0,
316 &pixel_refs);
317 EXPECT_TRUE(pixel_refs.empty());
320 // Layer sized iterators. These should find all 3 pixel refs.
322 std::vector<SkPixelRef*> pixel_refs;
323 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 1.0, &pixel_refs);
324 EXPECT_FALSE(pixel_refs.empty());
325 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
326 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[1][0].pixelRef());
327 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
328 EXPECT_EQ(3u, pixel_refs.size());
331 std::vector<SkPixelRef*> pixel_refs;
332 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
333 EXPECT_FALSE(pixel_refs.empty());
334 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
335 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[1][0].pixelRef());
336 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
337 EXPECT_EQ(3u, pixel_refs.size());
340 std::vector<SkPixelRef*> pixel_refs;
341 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
342 EXPECT_FALSE(pixel_refs.empty());
343 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
344 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[1][0].pixelRef());
345 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
346 EXPECT_EQ(3u, pixel_refs.size());
350 TYPED_TEST(RecordingSourceTest, DiscardablePixelRefsBaseNonDiscardable) {
351 gfx::Size grid_cell_size(256, 256);
352 gfx::Rect recorded_viewport(0, 0, 512, 512);
354 scoped_ptr<TypeParam> recording_source =
355 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
357 SkBitmap non_discardable_bitmap;
358 CreateBitmap(gfx::Size(512, 512), "notdiscardable", &non_discardable_bitmap);
360 SkBitmap discardable_bitmap[2][2];
361 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[0][0]);
362 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[0][1]);
363 CreateBitmap(gfx::Size(128, 128), "discardable", &discardable_bitmap[1][1]);
365 // One large non-discardable bitmap covers the whole grid.
366 // Discardable pixel refs are found in the following cells:
367 // |---|---|
368 // | x | x |
369 // |---|---|
370 // | | x |
371 // |---|---|
372 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 0));
373 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
374 recording_source->add_draw_bitmap(discardable_bitmap[0][1],
375 gfx::Point(260, 0));
376 recording_source->add_draw_bitmap(discardable_bitmap[1][1],
377 gfx::Point(260, 260));
378 recording_source->SetGatherPixelRefs(true);
379 recording_source->Rerecord();
381 scoped_refptr<RasterSource> raster_source =
382 CreateRasterSource<TypeParam>(recording_source.get());
384 // Tile sized iterators. These should find only one pixel ref.
386 std::vector<SkPixelRef*> pixel_refs;
387 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 1.0, &pixel_refs);
388 EXPECT_FALSE(pixel_refs.empty());
389 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
390 EXPECT_EQ(1u, pixel_refs.size());
393 std::vector<SkPixelRef*> pixel_refs;
394 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
395 EXPECT_FALSE(pixel_refs.empty());
396 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
397 EXPECT_EQ(1u, pixel_refs.size());
400 std::vector<SkPixelRef*> pixel_refs;
401 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
402 EXPECT_FALSE(pixel_refs.empty());
403 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
404 EXPECT_EQ(1u, pixel_refs.size());
406 // Shifted tile sized iterators. These should find only one pixel ref.
408 std::vector<SkPixelRef*> pixel_refs;
409 raster_source->GatherPixelRefs(gfx::Rect(260, 260, 256, 256), 1.0,
410 &pixel_refs);
411 EXPECT_FALSE(pixel_refs.empty());
412 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
413 EXPECT_EQ(1u, pixel_refs.size());
416 std::vector<SkPixelRef*> pixel_refs;
417 raster_source->GatherPixelRefs(gfx::Rect(520, 520, 512, 512), 2.0,
418 &pixel_refs);
419 EXPECT_FALSE(pixel_refs.empty());
420 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
421 EXPECT_EQ(1u, pixel_refs.size());
424 std::vector<SkPixelRef*> pixel_refs;
425 raster_source->GatherPixelRefs(gfx::Rect(130, 130, 128, 128), 0.5,
426 &pixel_refs);
427 EXPECT_FALSE(pixel_refs.empty());
428 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[1][1].pixelRef());
429 EXPECT_EQ(1u, pixel_refs.size());
431 // Ensure there's no discardable pixel refs in the empty cell
433 std::vector<SkPixelRef*> pixel_refs;
434 raster_source->GatherPixelRefs(gfx::Rect(0, 256, 256, 256), 1.0,
435 &pixel_refs);
436 EXPECT_TRUE(pixel_refs.empty());
438 // Layer sized iterators. These should find three pixel ref.
440 std::vector<SkPixelRef*> pixel_refs;
441 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 1.0, &pixel_refs);
442 EXPECT_FALSE(pixel_refs.empty());
443 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
444 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[0][1].pixelRef());
445 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
446 EXPECT_EQ(3u, pixel_refs.size());
449 std::vector<SkPixelRef*> pixel_refs;
450 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 1024, 1024), 2.0,
451 &pixel_refs);
452 EXPECT_FALSE(pixel_refs.empty());
453 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
454 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[0][1].pixelRef());
455 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
456 EXPECT_EQ(3u, pixel_refs.size());
459 std::vector<SkPixelRef*> pixel_refs;
460 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 0.5, &pixel_refs);
461 EXPECT_FALSE(pixel_refs.empty());
462 EXPECT_TRUE(pixel_refs[0] == discardable_bitmap[0][0].pixelRef());
463 EXPECT_TRUE(pixel_refs[1] == discardable_bitmap[0][1].pixelRef());
464 EXPECT_TRUE(pixel_refs[2] == discardable_bitmap[1][1].pixelRef());
465 EXPECT_EQ(3u, pixel_refs.size());
469 } // namespace
470 } // namespace cc