cc: Add tests for DisplayListRasterSource.
[chromium-blink-merge.git] / cc / playback / display_list_raster_source_unittest.cc
blobe41bc64a45f57bd9a825a9b2aab1ab903679a6b9
1 // Copyright 2013 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 "base/memory/scoped_ptr.h"
6 #include "cc/playback/display_list_raster_source.h"
7 #include "cc/test/fake_display_list_recording_source.h"
8 #include "cc/test/skia_common.h"
9 #include "skia/ext/refptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkPixelRef.h"
12 #include "third_party/skia/include/core/SkShader.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size_conversions.h"
16 namespace cc {
17 namespace {
19 TEST(DisplayListRasterSourceTest, AnalyzeIsSolidUnscaled) {
20 gfx::Size layer_bounds(400, 400);
22 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
23 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
25 SkPaint solid_paint;
26 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
27 solid_paint.setColor(solid_color);
29 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
30 SkPaint non_solid_paint;
31 non_solid_paint.setColor(non_solid_color);
33 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
34 solid_paint);
35 recording_source->Rerecord();
37 scoped_refptr<DisplayListRasterSource> raster =
38 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
39 recording_source.get(), false);
41 // Ensure everything is solid.
42 for (int y = 0; y <= 300; y += 100) {
43 for (int x = 0; x <= 300; x += 100) {
44 RasterSource::SolidColorAnalysis analysis;
45 gfx::Rect rect(x, y, 100, 100);
46 raster->PerformSolidColorAnalysis(rect, 1.0, &analysis);
47 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
48 EXPECT_EQ(solid_color, analysis.solid_color) << rect.ToString();
52 // Add one non-solid pixel and recreate the raster source.
53 recording_source->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1),
54 non_solid_paint);
55 recording_source->Rerecord();
56 raster = DisplayListRasterSource::CreateFromDisplayListRecordingSource(
57 recording_source.get(), false);
59 RasterSource::SolidColorAnalysis analysis;
60 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 100, 100), 1.0, &analysis);
61 EXPECT_FALSE(analysis.is_solid_color);
63 raster->PerformSolidColorAnalysis(gfx::Rect(100, 0, 100, 100), 1.0,
64 &analysis);
65 EXPECT_TRUE(analysis.is_solid_color);
66 EXPECT_EQ(solid_color, analysis.solid_color);
68 // Boundaries should be clipped.
69 analysis.is_solid_color = false;
70 raster->PerformSolidColorAnalysis(gfx::Rect(350, 0, 100, 100), 1.0,
71 &analysis);
72 EXPECT_TRUE(analysis.is_solid_color);
73 EXPECT_EQ(solid_color, analysis.solid_color);
75 analysis.is_solid_color = false;
76 raster->PerformSolidColorAnalysis(gfx::Rect(0, 350, 100, 100), 1.0,
77 &analysis);
78 EXPECT_TRUE(analysis.is_solid_color);
79 EXPECT_EQ(solid_color, analysis.solid_color);
81 analysis.is_solid_color = false;
82 raster->PerformSolidColorAnalysis(gfx::Rect(350, 350, 100, 100), 1.0,
83 &analysis);
84 EXPECT_TRUE(analysis.is_solid_color);
85 EXPECT_EQ(solid_color, analysis.solid_color);
88 TEST(DisplayListRasterSourceTest, AnalyzeIsSolidScaled) {
89 gfx::Size layer_bounds(400, 400);
91 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
92 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
94 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34);
95 SkPaint solid_paint;
96 solid_paint.setColor(solid_color);
98 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67);
99 SkPaint non_solid_paint;
100 non_solid_paint.setColor(non_solid_color);
102 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400),
103 solid_paint);
104 recording_source->Rerecord();
106 scoped_refptr<DisplayListRasterSource> raster =
107 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
108 recording_source.get(), false);
110 // Ensure everything is solid.
111 for (int y = 0; y <= 30; y += 10) {
112 for (int x = 0; x <= 30; x += 10) {
113 RasterSource::SolidColorAnalysis analysis;
114 gfx::Rect rect(x, y, 10, 10);
115 raster->PerformSolidColorAnalysis(rect, 0.1f, &analysis);
116 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString();
117 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString();
121 // Add one non-solid pixel and recreate the raster source.
122 recording_source->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1),
123 non_solid_paint);
124 recording_source->Rerecord();
125 raster = DisplayListRasterSource::CreateFromDisplayListRecordingSource(
126 recording_source.get(), false);
128 RasterSource::SolidColorAnalysis analysis;
129 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 10, 10), 0.1f, &analysis);
130 EXPECT_FALSE(analysis.is_solid_color);
132 raster->PerformSolidColorAnalysis(gfx::Rect(10, 0, 10, 10), 0.1f, &analysis);
133 EXPECT_TRUE(analysis.is_solid_color);
134 EXPECT_EQ(analysis.solid_color, solid_color);
136 // Boundaries should be clipped.
137 analysis.is_solid_color = false;
138 raster->PerformSolidColorAnalysis(gfx::Rect(35, 0, 10, 10), 0.1f, &analysis);
139 EXPECT_TRUE(analysis.is_solid_color);
140 EXPECT_EQ(analysis.solid_color, solid_color);
142 analysis.is_solid_color = false;
143 raster->PerformSolidColorAnalysis(gfx::Rect(0, 35, 10, 10), 0.1f, &analysis);
144 EXPECT_TRUE(analysis.is_solid_color);
145 EXPECT_EQ(analysis.solid_color, solid_color);
147 analysis.is_solid_color = false;
148 raster->PerformSolidColorAnalysis(gfx::Rect(35, 35, 10, 10), 0.1f, &analysis);
149 EXPECT_TRUE(analysis.is_solid_color);
150 EXPECT_EQ(analysis.solid_color, solid_color);
153 TEST(DisplayListRasterSourceTest, AnalyzeIsSolidEmpty) {
154 gfx::Size layer_bounds(400, 400);
156 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
157 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
158 recording_source->Rerecord();
160 scoped_refptr<DisplayListRasterSource> raster =
161 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
162 recording_source.get(), false);
163 RasterSource::SolidColorAnalysis analysis;
164 EXPECT_FALSE(analysis.is_solid_color);
166 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 400, 400), 1.f, &analysis);
168 EXPECT_TRUE(analysis.is_solid_color);
169 EXPECT_EQ(analysis.solid_color, SkColorSetARGB(0, 0, 0, 0));
172 TEST(DisplayListRasterSourceTest, PixelRefIteratorDiscardableRefsOneTile) {
173 gfx::Size layer_bounds(512, 512);
175 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
176 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
178 SkBitmap discardable_bitmap[2][2];
179 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][0]);
180 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][1]);
181 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][1]);
183 // Discardable pixel refs are found in the following cells:
184 // |---|---|
185 // | x | x |
186 // |---|---|
187 // | | x |
188 // |---|---|
189 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
190 recording_source->add_draw_bitmap(discardable_bitmap[0][1],
191 gfx::Point(260, 0));
192 recording_source->add_draw_bitmap(discardable_bitmap[1][1],
193 gfx::Point(260, 260));
194 recording_source->SetGatherPixelRefs(true);
195 recording_source->Rerecord();
197 scoped_refptr<DisplayListRasterSource> raster =
198 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
199 recording_source.get(), false);
201 // Tile sized iterators. These should find only one pixel ref.
203 std::vector<SkPixelRef*> pixel_refs;
204 raster->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 1.0, &pixel_refs);
205 EXPECT_EQ(1u, pixel_refs.size());
206 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
209 std::vector<SkPixelRef*> pixel_refs;
210 raster->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
211 EXPECT_EQ(1u, pixel_refs.size());
212 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
215 std::vector<SkPixelRef*> pixel_refs;
216 raster->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
217 EXPECT_EQ(1u, pixel_refs.size());
218 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
220 // Shifted tile sized iterators. These should find only one pixel ref.
222 std::vector<SkPixelRef*> pixel_refs;
223 raster->GatherPixelRefs(gfx::Rect(260, 260, 256, 256), 1.0, &pixel_refs);
224 EXPECT_EQ(1u, pixel_refs.size());
225 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0]);
228 std::vector<SkPixelRef*> pixel_refs;
229 raster->GatherPixelRefs(gfx::Rect(520, 520, 512, 512), 2.0, &pixel_refs);
230 EXPECT_EQ(1u, pixel_refs.size());
231 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0]);
234 std::vector<SkPixelRef*> pixel_refs;
235 raster->GatherPixelRefs(gfx::Rect(130, 130, 128, 128), 0.5, &pixel_refs);
236 EXPECT_EQ(1u, pixel_refs.size());
237 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0]);
239 // Ensure there's no discardable pixel refs in the empty cell
241 std::vector<SkPixelRef*> pixel_refs;
242 raster->GatherPixelRefs(gfx::Rect(0, 256, 256, 256), 1.0, &pixel_refs);
243 EXPECT_EQ(0u, pixel_refs.size());
245 // Layer sized iterators. These should find three pixel ref.
247 std::vector<SkPixelRef*> pixel_refs;
248 raster->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 1.0, &pixel_refs);
249 EXPECT_EQ(3u, pixel_refs.size());
250 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
251 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1]);
252 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2]);
255 std::vector<SkPixelRef*> pixel_refs;
256 raster->GatherPixelRefs(gfx::Rect(0, 0, 1024, 1024), 2.0, &pixel_refs);
257 EXPECT_EQ(3u, pixel_refs.size());
258 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
259 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1]);
260 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2]);
263 std::vector<SkPixelRef*> pixel_refs;
264 raster->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 0.5, &pixel_refs);
265 EXPECT_EQ(3u, pixel_refs.size());
266 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0]);
267 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1]);
268 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2]);
272 TEST(DisplayListRasterSourceTest, RasterFullContents) {
273 gfx::Size layer_bounds(3, 5);
274 float contents_scale = 1.5f;
275 float raster_divisions = 2.f;
277 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
278 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
279 recording_source->SetBackgroundColor(SK_ColorBLACK);
280 recording_source->SetClearCanvasWithDebugColor(false);
282 // Because the caller sets content opaque, it also promises that it
283 // has at least filled in layer_bounds opaquely.
284 SkPaint white_paint;
285 white_paint.setColor(SK_ColorWHITE);
286 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
287 white_paint);
288 recording_source->Rerecord();
290 scoped_refptr<DisplayListRasterSource> raster =
291 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
292 recording_source.get(), false);
294 gfx::Size content_bounds(
295 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
297 // Simulate drawing into different tiles at different offsets.
298 int step_x = std::ceil(content_bounds.width() / raster_divisions);
299 int step_y = std::ceil(content_bounds.height() / raster_divisions);
300 for (int offset_x = 0; offset_x < content_bounds.width();
301 offset_x += step_x) {
302 for (int offset_y = 0; offset_y < content_bounds.height();
303 offset_y += step_y) {
304 gfx::Rect content_rect(offset_x, offset_y, step_x, step_y);
305 content_rect.Intersect(gfx::Rect(content_bounds));
307 // Simulate a canvas rect larger than the content rect. Every pixel
308 // up to one pixel outside the content rect is guaranteed to be opaque.
309 // Outside of that is undefined.
310 gfx::Rect canvas_rect(content_rect);
311 canvas_rect.Inset(0, 0, -1, -1);
313 SkBitmap bitmap;
314 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
315 SkCanvas canvas(bitmap);
316 canvas.clear(SK_ColorTRANSPARENT);
318 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect,
319 contents_scale);
321 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
322 int num_pixels = bitmap.width() * bitmap.height();
323 bool all_white = true;
324 for (int i = 0; i < num_pixels; ++i) {
325 EXPECT_EQ(SkColorGetA(pixels[i]), 255u);
326 all_white &= (SkColorGetR(pixels[i]) == 255);
327 all_white &= (SkColorGetG(pixels[i]) == 255);
328 all_white &= (SkColorGetB(pixels[i]) == 255);
331 // If the canvas doesn't extend past the edge of the content,
332 // it should be entirely white. Otherwise, the edge of the content
333 // will be non-white.
334 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect));
339 TEST(DisplayListRasterSourceTest, RasterPartialContents) {
340 gfx::Size layer_bounds(3, 5);
341 float contents_scale = 1.5f;
343 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
344 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
345 recording_source->SetBackgroundColor(SK_ColorGREEN);
346 recording_source->SetClearCanvasWithDebugColor(false);
348 // First record everything as white.
349 SkPaint white_paint;
350 white_paint.setColor(SK_ColorWHITE);
351 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
352 white_paint);
353 recording_source->Rerecord();
355 scoped_refptr<DisplayListRasterSource> raster =
356 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
357 recording_source.get(), false);
359 gfx::Size content_bounds(
360 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
362 SkBitmap bitmap;
363 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height());
364 SkCanvas canvas(bitmap);
365 canvas.clear(SK_ColorTRANSPARENT);
367 // Playback the full rect which should make everything white.
368 gfx::Rect raster_full_rect(content_bounds);
369 gfx::Rect playback_rect(content_bounds);
370 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
371 contents_scale);
374 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
375 for (int i = 0; i < bitmap.width(); ++i) {
376 for (int j = 0; j < bitmap.height(); ++j) {
377 SCOPED_TRACE(i);
378 SCOPED_TRACE(j);
379 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
380 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()]));
381 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()]));
382 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()]));
387 // Re-record everything as black.
388 SkPaint black_paint;
389 black_paint.setColor(SK_ColorBLACK);
390 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
391 black_paint);
392 recording_source->Rerecord();
394 // Make a new RasterSource from the new recording.
395 raster = DisplayListRasterSource::CreateFromDisplayListRecordingSource(
396 recording_source.get(), false);
398 // We're going to playback from "everything is black" into a smaller area.
399 playback_rect.Inset(1, 2);
400 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
401 contents_scale);
403 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
404 int num_black = 0;
405 int num_white = 0;
406 for (int i = 0; i < bitmap.width(); ++i) {
407 for (int j = 0; j < bitmap.height(); ++j) {
408 SCOPED_TRACE(i);
409 SCOPED_TRACE(j);
410 bool expect_black = playback_rect.Contains(i, j);
411 if (expect_black) {
412 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
413 EXPECT_EQ(0u, SkColorGetR(pixels[i + j * bitmap.width()]));
414 EXPECT_EQ(0u, SkColorGetG(pixels[i + j * bitmap.width()]));
415 EXPECT_EQ(0u, SkColorGetB(pixels[i + j * bitmap.width()]));
416 ++num_black;
417 } else {
418 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
419 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()]));
420 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()]));
421 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()]));
422 ++num_white;
426 EXPECT_GT(num_black, 0);
427 EXPECT_GT(num_white, 0);
430 TEST(DisplayListRasterSourceTest, RasterContentsTransparent) {
431 gfx::Size layer_bounds(5, 3);
432 float contents_scale = 0.5f;
434 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
435 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
436 recording_source->SetBackgroundColor(SK_ColorTRANSPARENT);
437 recording_source->SetRequiresClear(true);
438 recording_source->SetClearCanvasWithDebugColor(false);
439 recording_source->Rerecord();
441 scoped_refptr<DisplayListRasterSource> raster =
442 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
443 recording_source.get(), false);
444 gfx::Size content_bounds(
445 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
447 gfx::Rect canvas_rect(content_bounds);
448 canvas_rect.Inset(0, 0, -1, -1);
450 SkBitmap bitmap;
451 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
452 SkCanvas canvas(bitmap);
454 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect, contents_scale);
456 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
457 int num_pixels = bitmap.width() * bitmap.height();
458 for (int i = 0; i < num_pixels; ++i) {
459 EXPECT_EQ(SkColorGetA(pixels[i]), 0u);
463 } // namespace
464 } // namespace cc