Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / playback / display_list_raster_source_unittest.cc
blob922ba00a9296fe3367592fde9bd4a26feb9d7dea
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 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap[0][0]);
180 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap[0][1]);
181 CreateDiscardableBitmap(gfx::Size(32, 32), &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<skia::PositionPixelRef> 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].pixel_ref);
207 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
208 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
211 std::vector<skia::PositionPixelRef> pixel_refs;
212 raster->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 2.0, &pixel_refs);
213 EXPECT_EQ(1u, pixel_refs.size());
214 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0].pixel_ref);
215 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
216 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
219 std::vector<skia::PositionPixelRef> pixel_refs;
220 raster->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), 0.5, &pixel_refs);
221 EXPECT_EQ(1u, pixel_refs.size());
222 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0].pixel_ref);
223 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
224 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
226 // Shifted tile sized iterators. These should find only one pixel ref.
228 std::vector<skia::PositionPixelRef> pixel_refs;
229 raster->GatherPixelRefs(gfx::Rect(260, 260, 256, 256), 1.0, &pixel_refs);
230 EXPECT_EQ(1u, pixel_refs.size());
231 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0].pixel_ref);
232 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
233 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
236 std::vector<skia::PositionPixelRef> pixel_refs;
237 raster->GatherPixelRefs(gfx::Rect(520, 520, 512, 512), 2.0, &pixel_refs);
238 EXPECT_EQ(1u, pixel_refs.size());
239 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0].pixel_ref);
240 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
241 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
244 std::vector<skia::PositionPixelRef> pixel_refs;
245 raster->GatherPixelRefs(gfx::Rect(130, 130, 128, 128), 0.5, &pixel_refs);
246 EXPECT_EQ(1u, pixel_refs.size());
247 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[0].pixel_ref);
248 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
249 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
251 // Ensure there's no discardable pixel refs in the empty cell
253 std::vector<skia::PositionPixelRef> pixel_refs;
254 raster->GatherPixelRefs(gfx::Rect(0, 256, 256, 256), 1.0, &pixel_refs);
255 EXPECT_EQ(0u, pixel_refs.size());
257 // Layer sized iterators. These should find three pixel ref.
259 std::vector<skia::PositionPixelRef> pixel_refs;
260 raster->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), 1.0, &pixel_refs);
261 EXPECT_EQ(3u, pixel_refs.size());
262 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0].pixel_ref);
263 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1].pixel_ref);
264 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2].pixel_ref);
265 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
266 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
267 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
268 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect).ToString());
269 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
270 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect).ToString());
273 std::vector<skia::PositionPixelRef> pixel_refs;
274 raster->GatherPixelRefs(gfx::Rect(0, 0, 1024, 1024), 2.0, &pixel_refs);
275 EXPECT_EQ(3u, pixel_refs.size());
276 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0].pixel_ref);
277 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1].pixel_ref);
278 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2].pixel_ref);
279 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
280 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
281 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
282 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect).ToString());
283 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
284 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect).ToString());
287 std::vector<skia::PositionPixelRef> pixel_refs;
288 raster->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), 0.5, &pixel_refs);
289 EXPECT_EQ(3u, pixel_refs.size());
290 EXPECT_EQ(discardable_bitmap[0][0].pixelRef(), pixel_refs[0].pixel_ref);
291 EXPECT_EQ(discardable_bitmap[0][1].pixelRef(), pixel_refs[1].pixel_ref);
292 EXPECT_EQ(discardable_bitmap[1][1].pixelRef(), pixel_refs[2].pixel_ref);
293 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
294 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
295 EXPECT_EQ(gfx::RectF(260, 0, 32, 32).ToString(),
296 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect).ToString());
297 EXPECT_EQ(gfx::RectF(260, 260, 32, 32).ToString(),
298 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect).ToString());
302 TEST(DisplayListRasterSourceTest, RasterFullContents) {
303 gfx::Size layer_bounds(3, 5);
304 float contents_scale = 1.5f;
305 float raster_divisions = 2.f;
307 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
308 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
309 recording_source->SetBackgroundColor(SK_ColorBLACK);
310 recording_source->SetClearCanvasWithDebugColor(false);
312 // Because the caller sets content opaque, it also promises that it
313 // has at least filled in layer_bounds opaquely.
314 SkPaint white_paint;
315 white_paint.setColor(SK_ColorWHITE);
316 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
317 white_paint);
318 recording_source->Rerecord();
320 scoped_refptr<DisplayListRasterSource> raster =
321 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
322 recording_source.get(), false);
324 gfx::Size content_bounds(
325 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
327 // Simulate drawing into different tiles at different offsets.
328 int step_x = std::ceil(content_bounds.width() / raster_divisions);
329 int step_y = std::ceil(content_bounds.height() / raster_divisions);
330 for (int offset_x = 0; offset_x < content_bounds.width();
331 offset_x += step_x) {
332 for (int offset_y = 0; offset_y < content_bounds.height();
333 offset_y += step_y) {
334 gfx::Rect content_rect(offset_x, offset_y, step_x, step_y);
335 content_rect.Intersect(gfx::Rect(content_bounds));
337 // Simulate a canvas rect larger than the content rect. Every pixel
338 // up to one pixel outside the content rect is guaranteed to be opaque.
339 // Outside of that is undefined.
340 gfx::Rect canvas_rect(content_rect);
341 canvas_rect.Inset(0, 0, -1, -1);
343 SkBitmap bitmap;
344 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
345 SkCanvas canvas(bitmap);
346 canvas.clear(SK_ColorTRANSPARENT);
348 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect,
349 contents_scale);
351 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
352 int num_pixels = bitmap.width() * bitmap.height();
353 bool all_white = true;
354 for (int i = 0; i < num_pixels; ++i) {
355 EXPECT_EQ(SkColorGetA(pixels[i]), 255u);
356 all_white &= (SkColorGetR(pixels[i]) == 255);
357 all_white &= (SkColorGetG(pixels[i]) == 255);
358 all_white &= (SkColorGetB(pixels[i]) == 255);
361 // If the canvas doesn't extend past the edge of the content,
362 // it should be entirely white. Otherwise, the edge of the content
363 // will be non-white.
364 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect));
369 TEST(DisplayListRasterSourceTest, RasterPartialContents) {
370 gfx::Size layer_bounds(3, 5);
371 float contents_scale = 1.5f;
373 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
374 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
375 recording_source->SetBackgroundColor(SK_ColorGREEN);
376 recording_source->SetClearCanvasWithDebugColor(false);
378 // First record everything as white.
379 SkPaint white_paint;
380 white_paint.setColor(SK_ColorWHITE);
381 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
382 white_paint);
383 recording_source->Rerecord();
385 scoped_refptr<DisplayListRasterSource> raster =
386 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
387 recording_source.get(), false);
389 gfx::Size content_bounds(
390 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
392 SkBitmap bitmap;
393 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height());
394 SkCanvas canvas(bitmap);
395 canvas.clear(SK_ColorTRANSPARENT);
397 // Playback the full rect which should make everything white.
398 gfx::Rect raster_full_rect(content_bounds);
399 gfx::Rect playback_rect(content_bounds);
400 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
401 contents_scale);
404 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
405 for (int i = 0; i < bitmap.width(); ++i) {
406 for (int j = 0; j < bitmap.height(); ++j) {
407 SCOPED_TRACE(i);
408 SCOPED_TRACE(j);
409 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
410 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()]));
411 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()]));
412 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()]));
417 // Re-record everything as black.
418 SkPaint black_paint;
419 black_paint.setColor(SK_ColorBLACK);
420 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
421 black_paint);
422 recording_source->Rerecord();
424 // Make a new RasterSource from the new recording.
425 raster = DisplayListRasterSource::CreateFromDisplayListRecordingSource(
426 recording_source.get(), false);
428 // We're going to playback from "everything is black" into a smaller area,
429 // that touches the edge pixels of the recording.
430 playback_rect.Inset(1, 2, 0, 1);
431 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
432 contents_scale);
434 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
435 int num_black = 0;
436 int num_white = 0;
437 for (int i = 0; i < bitmap.width(); ++i) {
438 for (int j = 0; j < bitmap.height(); ++j) {
439 SCOPED_TRACE(j);
440 SCOPED_TRACE(i);
441 bool expect_black = playback_rect.Contains(i, j);
442 if (expect_black) {
443 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
444 EXPECT_EQ(0u, SkColorGetR(pixels[i + j * bitmap.width()]));
445 EXPECT_EQ(0u, SkColorGetG(pixels[i + j * bitmap.width()]));
446 EXPECT_EQ(0u, SkColorGetB(pixels[i + j * bitmap.width()]));
447 ++num_black;
448 } else {
449 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()]));
450 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()]));
451 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()]));
452 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()]));
453 ++num_white;
457 EXPECT_GT(num_black, 0);
458 EXPECT_GT(num_white, 0);
461 TEST(DisplayListRasterSourceTest, RasterPartialClear) {
462 gfx::Size layer_bounds(3, 5);
463 gfx::Size partial_bounds(2, 4);
464 float contents_scale = 1.5f;
466 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
467 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
468 recording_source->SetBackgroundColor(SK_ColorGREEN);
469 recording_source->SetRequiresClear(true);
470 recording_source->SetClearCanvasWithDebugColor(false);
472 // First record everything as white.
473 const unsigned alpha_dark = 10u;
474 SkPaint white_paint;
475 white_paint.setColor(SK_ColorWHITE);
476 white_paint.setAlpha(alpha_dark);
477 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
478 white_paint);
479 recording_source->Rerecord();
481 scoped_refptr<DisplayListRasterSource> raster =
482 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
483 recording_source.get(), false);
485 gfx::Size content_bounds(
486 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
488 SkBitmap bitmap;
489 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height());
490 SkCanvas canvas(bitmap);
491 canvas.clear(SK_ColorTRANSPARENT);
493 // Playback the full rect which should make everything light gray (alpha=10).
494 gfx::Rect raster_full_rect(content_bounds);
495 gfx::Rect playback_rect(content_bounds);
496 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
497 contents_scale);
500 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
501 for (int i = 0; i < bitmap.width(); ++i) {
502 for (int j = 0; j < bitmap.height(); ++j) {
503 SCOPED_TRACE(i);
504 SCOPED_TRACE(j);
505 EXPECT_EQ(alpha_dark, SkColorGetA(pixels[i + j * bitmap.width()]));
506 EXPECT_EQ(alpha_dark, SkColorGetR(pixels[i + j * bitmap.width()]));
507 EXPECT_EQ(alpha_dark, SkColorGetG(pixels[i + j * bitmap.width()]));
508 EXPECT_EQ(alpha_dark, SkColorGetB(pixels[i + j * bitmap.width()]));
513 scoped_ptr<FakeDisplayListRecordingSource> recording_source_light =
514 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
515 recording_source_light->SetBackgroundColor(SK_ColorGREEN);
516 recording_source_light->SetRequiresClear(true);
517 recording_source_light->SetClearCanvasWithDebugColor(false);
519 // Record everything as a slightly lighter white.
520 const unsigned alpha_light = 18u;
521 white_paint.setAlpha(alpha_light);
522 recording_source_light->add_draw_rect_with_paint(gfx::Rect(layer_bounds),
523 white_paint);
524 recording_source_light->Rerecord();
526 // Make a new RasterSource from the new recording.
527 raster = DisplayListRasterSource::CreateFromDisplayListRecordingSource(
528 recording_source_light.get(), false);
530 // We're going to playback from alpha(18) white rectangle into a smaller area
531 // of the recording resulting in a smaller lighter white rectangle over a
532 // darker white background rectangle.
533 playback_rect = gfx::Rect(
534 gfx::ToCeiledSize(gfx::ScaleSize(partial_bounds, contents_scale)));
535 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect,
536 contents_scale);
538 // Test that the whole playback_rect was cleared and repainted with new alpha.
539 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
540 for (int i = 0; i < playback_rect.width(); ++i) {
541 for (int j = 0; j < playback_rect.height(); ++j) {
542 SCOPED_TRACE(j);
543 SCOPED_TRACE(i);
544 EXPECT_EQ(alpha_light, SkColorGetA(pixels[i + j * bitmap.width()]));
545 EXPECT_EQ(alpha_light, SkColorGetR(pixels[i + j * bitmap.width()]));
546 EXPECT_EQ(alpha_light, SkColorGetG(pixels[i + j * bitmap.width()]));
547 EXPECT_EQ(alpha_light, SkColorGetB(pixels[i + j * bitmap.width()]));
552 TEST(DisplayListRasterSourceTest, RasterContentsTransparent) {
553 gfx::Size layer_bounds(5, 3);
554 float contents_scale = 0.5f;
556 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
557 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
558 recording_source->SetBackgroundColor(SK_ColorTRANSPARENT);
559 recording_source->SetRequiresClear(true);
560 recording_source->SetClearCanvasWithDebugColor(false);
561 recording_source->Rerecord();
563 scoped_refptr<DisplayListRasterSource> raster =
564 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
565 recording_source.get(), false);
566 gfx::Size content_bounds(
567 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
569 gfx::Rect canvas_rect(content_bounds);
570 canvas_rect.Inset(0, 0, -1, -1);
572 SkBitmap bitmap;
573 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height());
574 SkCanvas canvas(bitmap);
576 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect, contents_scale);
578 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels());
579 int num_pixels = bitmap.width() * bitmap.height();
580 for (int i = 0; i < num_pixels; ++i) {
581 EXPECT_EQ(SkColorGetA(pixels[i]), 0u);
585 TEST(DisplayListRasterSourceTest,
586 GetPictureMemoryUsageIncludesClientReportedMemory) {
587 const size_t kReportedMemoryUsageInBytes = 100 * 1024 * 1024;
588 gfx::Size layer_bounds(5, 3);
589 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
590 FakeDisplayListRecordingSource::CreateFilledRecordingSource(layer_bounds);
591 recording_source->set_reported_memory_usage(kReportedMemoryUsageInBytes);
592 recording_source->Rerecord();
594 scoped_refptr<DisplayListRasterSource> raster =
595 DisplayListRasterSource::CreateFromDisplayListRecordingSource(
596 recording_source.get(), false);
597 size_t total_memory_usage = raster->GetPictureMemoryUsage();
598 EXPECT_GE(total_memory_usage, kReportedMemoryUsageInBytes);
599 EXPECT_LT(total_memory_usage, 2 * kReportedMemoryUsageInBytes);
602 } // namespace
603 } // namespace cc