Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / playback / recording_source_unittest.cc
blob949fdce3c8d070ade36160a482e6019385475ea8
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/playback/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/skia_common.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace cc {
15 namespace {
17 template <class T>
18 scoped_ptr<T> CreateRecordingSource(const gfx::Rect& viewport,
19 const gfx::Size& grid_cell_size);
21 template <>
22 scoped_ptr<FakePicturePile> CreateRecordingSource<FakePicturePile>(
23 const gfx::Rect& viewport,
24 const gfx::Size& grid_cell_size) {
25 return FakePicturePile::CreateFilledPile(grid_cell_size, viewport.size());
28 template <>
29 scoped_ptr<FakeDisplayListRecordingSource> CreateRecordingSource<
30 FakeDisplayListRecordingSource>(const gfx::Rect& viewport,
31 const gfx::Size& grid_cell_size) {
32 gfx::Rect layer_rect(viewport.right(), viewport.bottom());
33 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
34 FakeDisplayListRecordingSource::CreateRecordingSource(viewport,
35 layer_rect.size());
36 recording_source->SetGridCellSize(grid_cell_size);
38 return recording_source.Pass();
41 template <class T>
42 scoped_refptr<RasterSource> CreateRasterSource(T* recording_source);
44 template <>
45 scoped_refptr<RasterSource> CreateRasterSource(
46 FakePicturePile* recording_source) {
47 return FakePicturePileImpl::CreateFromPile(recording_source, nullptr);
50 template <>
51 scoped_refptr<RasterSource> CreateRasterSource(
52 FakeDisplayListRecordingSource* recording_source) {
53 bool can_use_lcd_text = true;
54 return DisplayListRasterSource::CreateFromDisplayListRecordingSource(
55 recording_source, can_use_lcd_text);
58 template <typename T>
59 class RecordingSourceTest : public testing::Test {};
61 using testing::Types;
63 typedef Types<FakePicturePile, FakeDisplayListRecordingSource>
64 RecordingSourceImplementations;
66 TYPED_TEST_CASE(RecordingSourceTest, RecordingSourceImplementations);
68 TYPED_TEST(RecordingSourceTest, NoGatherImageEmptyImages) {
69 gfx::Size grid_cell_size(128, 128);
70 gfx::Rect recorded_viewport(0, 0, 256, 256);
72 scoped_ptr<TypeParam> recording_source =
73 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
74 recording_source->SetGatherDiscardableImages(false);
75 recording_source->Rerecord();
77 scoped_refptr<RasterSource> raster_source =
78 CreateRasterSource<TypeParam>(recording_source.get());
80 // If recording source do not gather images, raster source is not going to
81 // get images.
83 std::vector<skia::PositionImage> images;
84 raster_source->GatherDiscardableImages(recorded_viewport, &images);
85 EXPECT_TRUE(images.empty());
89 TYPED_TEST(RecordingSourceTest, EmptyImages) {
90 gfx::Size grid_cell_size(128, 128);
91 gfx::Rect recorded_viewport(0, 0, 256, 256);
93 scoped_ptr<TypeParam> recording_source =
94 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
95 recording_source->SetGatherDiscardableImages(true);
96 recording_source->Rerecord();
98 scoped_refptr<RasterSource> raster_source =
99 CreateRasterSource<TypeParam>(recording_source.get());
101 // Tile sized iterators.
103 std::vector<skia::PositionImage> images;
104 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 128, 128), &images);
105 EXPECT_TRUE(images.empty());
107 // Shifted tile sized iterators.
109 std::vector<skia::PositionImage> images;
110 raster_source->GatherDiscardableImages(gfx::Rect(140, 140, 128, 128),
111 &images);
112 EXPECT_TRUE(images.empty());
114 // Layer sized iterators.
116 std::vector<skia::PositionImage> images;
117 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 256, 256), &images);
118 EXPECT_TRUE(images.empty());
122 TYPED_TEST(RecordingSourceTest, NoDiscardableImages) {
123 gfx::Size grid_cell_size(128, 128);
124 gfx::Rect recorded_viewport(0, 0, 256, 256);
126 scoped_ptr<TypeParam> recording_source =
127 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
129 SkPaint simple_paint;
130 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
132 SkBitmap non_discardable_bitmap;
133 non_discardable_bitmap.allocN32Pixels(128, 128);
134 non_discardable_bitmap.setImmutable();
135 skia::RefPtr<SkImage> non_discardable_image =
136 skia::AdoptRef(SkImage::NewFromBitmap(non_discardable_bitmap));
138 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256),
139 simple_paint);
140 recording_source->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512),
141 simple_paint);
142 recording_source->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256),
143 simple_paint);
144 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256),
145 simple_paint);
146 recording_source->add_draw_image(non_discardable_image.get(),
147 gfx::Point(128, 0));
148 recording_source->add_draw_image(non_discardable_image.get(),
149 gfx::Point(0, 128));
150 recording_source->add_draw_image(non_discardable_image.get(),
151 gfx::Point(150, 150));
152 recording_source->SetGatherDiscardableImages(true);
153 recording_source->Rerecord();
155 scoped_refptr<RasterSource> raster_source =
156 CreateRasterSource<TypeParam>(recording_source.get());
158 // Tile sized iterators.
160 std::vector<skia::PositionImage> images;
161 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 128, 128), &images);
162 EXPECT_TRUE(images.empty());
164 // Shifted tile sized iterators.
166 std::vector<skia::PositionImage> images;
167 raster_source->GatherDiscardableImages(gfx::Rect(140, 140, 128, 128),
168 &images);
169 EXPECT_TRUE(images.empty());
171 // Layer sized iterators.
173 std::vector<skia::PositionImage> images;
174 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 256, 256), &images);
175 EXPECT_TRUE(images.empty());
179 TYPED_TEST(RecordingSourceTest, DiscardableImages) {
180 gfx::Size grid_cell_size(128, 128);
181 gfx::Rect recorded_viewport(0, 0, 256, 256);
183 scoped_ptr<TypeParam> recording_source =
184 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
186 skia::RefPtr<SkImage> discardable_image[2][2];
187 discardable_image[0][0] = CreateDiscardableImage(gfx::Size(32, 32));
188 discardable_image[1][0] = CreateDiscardableImage(gfx::Size(32, 32));
189 discardable_image[1][1] = CreateDiscardableImage(gfx::Size(32, 32));
191 // Discardable images are found in the following cells:
192 // |---|---|
193 // | x | |
194 // |---|---|
195 // | x | x |
196 // |---|---|
197 recording_source->add_draw_image(discardable_image[0][0].get(),
198 gfx::Point(0, 0));
199 recording_source->add_draw_image(discardable_image[1][0].get(),
200 gfx::Point(0, 130));
201 recording_source->add_draw_image(discardable_image[1][1].get(),
202 gfx::Point(140, 140));
203 recording_source->SetGatherDiscardableImages(true);
204 recording_source->Rerecord();
206 scoped_refptr<RasterSource> raster_source =
207 CreateRasterSource<TypeParam>(recording_source.get());
209 // Tile sized iterators. These should find only one image.
211 std::vector<skia::PositionImage> images;
212 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 128, 128), &images);
213 EXPECT_EQ(1u, images.size());
214 EXPECT_TRUE(images[0].image == discardable_image[0][0].get());
215 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
216 gfx::SkRectToRectF(images[0].image_rect).ToString());
219 // Shifted tile sized iterators. These should find only one image.
221 std::vector<skia::PositionImage> images;
222 raster_source->GatherDiscardableImages(gfx::Rect(140, 140, 128, 128),
223 &images);
224 EXPECT_EQ(1u, images.size());
225 EXPECT_TRUE(images[0].image == discardable_image[1][1].get());
226 EXPECT_EQ(gfx::RectF(140, 140, 32, 32).ToString(),
227 gfx::SkRectToRectF(images[0].image_rect).ToString());
230 // Ensure there's no discardable images in the empty cell
232 std::vector<skia::PositionImage> images;
233 raster_source->GatherDiscardableImages(gfx::Rect(140, 0, 128, 128),
234 &images);
235 EXPECT_TRUE(images.empty());
238 // Layer sized iterators. These should find all 3 images.
240 std::vector<skia::PositionImage> images;
241 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 256, 256), &images);
242 EXPECT_EQ(3u, images.size());
243 EXPECT_TRUE(images[0].image == discardable_image[0][0].get());
244 EXPECT_TRUE(images[1].image == discardable_image[1][0].get());
245 EXPECT_TRUE(images[2].image == discardable_image[1][1].get());
246 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
247 gfx::SkRectToRectF(images[0].image_rect).ToString());
248 EXPECT_EQ(gfx::RectF(0, 130, 32, 32).ToString(),
249 gfx::SkRectToRectF(images[1].image_rect).ToString());
250 EXPECT_EQ(gfx::RectF(140, 140, 32, 32).ToString(),
251 gfx::SkRectToRectF(images[2].image_rect).ToString());
255 TYPED_TEST(RecordingSourceTest, DiscardableImagesBaseNonDiscardable) {
256 gfx::Size grid_cell_size(256, 256);
257 gfx::Rect recorded_viewport(0, 0, 512, 512);
259 scoped_ptr<TypeParam> recording_source =
260 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
262 SkBitmap non_discardable_bitmap;
263 non_discardable_bitmap.allocN32Pixels(512, 512);
264 non_discardable_bitmap.setImmutable();
265 skia::RefPtr<SkImage> non_discardable_image =
266 skia::AdoptRef(SkImage::NewFromBitmap(non_discardable_bitmap));
268 skia::RefPtr<SkImage> discardable_image[2][2];
269 discardable_image[0][0] = CreateDiscardableImage(gfx::Size(128, 128));
270 discardable_image[0][1] = CreateDiscardableImage(gfx::Size(128, 128));
271 discardable_image[1][1] = CreateDiscardableImage(gfx::Size(128, 128));
273 // One large non-discardable image covers the whole grid.
274 // Discardable images are found in the following cells:
275 // |---|---|
276 // | x | x |
277 // |---|---|
278 // | | x |
279 // |---|---|
280 recording_source->add_draw_image(non_discardable_image.get(),
281 gfx::Point(0, 0));
282 recording_source->add_draw_image(discardable_image[0][0].get(),
283 gfx::Point(0, 0));
284 recording_source->add_draw_image(discardable_image[0][1].get(),
285 gfx::Point(260, 0));
286 recording_source->add_draw_image(discardable_image[1][1].get(),
287 gfx::Point(260, 260));
288 recording_source->SetGatherDiscardableImages(true);
289 recording_source->Rerecord();
291 scoped_refptr<RasterSource> raster_source =
292 CreateRasterSource<TypeParam>(recording_source.get());
294 // Tile sized iterators. These should find only one image.
296 std::vector<skia::PositionImage> images;
297 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 256, 256), &images);
298 EXPECT_EQ(1u, images.size());
299 EXPECT_TRUE(images[0].image == discardable_image[0][0].get());
300 EXPECT_EQ(gfx::RectF(128, 128).ToString(),
301 gfx::SkRectToRectF(images[0].image_rect).ToString());
303 // Shifted tile sized iterators. These should find only one image.
305 std::vector<skia::PositionImage> images;
306 raster_source->GatherDiscardableImages(gfx::Rect(260, 260, 256, 256),
307 &images);
308 EXPECT_EQ(1u, images.size());
309 EXPECT_TRUE(images[0].image == discardable_image[1][1].get());
310 EXPECT_EQ(gfx::RectF(260, 260, 128, 128).ToString(),
311 gfx::SkRectToRectF(images[0].image_rect).ToString());
313 // Ensure there's no discardable images in the empty cell
315 std::vector<skia::PositionImage> images;
316 raster_source->GatherDiscardableImages(gfx::Rect(0, 256, 256, 256),
317 &images);
318 EXPECT_TRUE(images.empty());
320 // Layer sized iterators. These should find three images.
322 std::vector<skia::PositionImage> images;
323 raster_source->GatherDiscardableImages(gfx::Rect(0, 0, 512, 512), &images);
324 EXPECT_EQ(3u, images.size());
325 EXPECT_TRUE(images[0].image == discardable_image[0][0].get());
326 EXPECT_TRUE(images[1].image == discardable_image[0][1].get());
327 EXPECT_TRUE(images[2].image == discardable_image[1][1].get());
328 EXPECT_EQ(gfx::RectF(128, 128).ToString(),
329 gfx::SkRectToRectF(images[0].image_rect).ToString());
330 EXPECT_EQ(gfx::RectF(260, 0, 128, 128).ToString(),
331 gfx::SkRectToRectF(images[1].image_rect).ToString());
332 EXPECT_EQ(gfx::RectF(260, 260, 128, 128).ToString(),
333 gfx::SkRectToRectF(images[2].image_rect).ToString());
337 } // namespace
338 } // namespace cc