| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "cc/test/fake_picture_pile_impl.h" | |
| 7 #include "cc/test/skia_common.h" | |
| 8 #include "skia/ext/refptr.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/skia/include/core/SkPixelRef.h" | |
| 11 #include "third_party/skia/include/core/SkShader.h" | |
| 12 #include "ui/gfx/geometry/rect.h" | |
| 13 #include "ui/gfx/geometry/size_conversions.h" | |
| 14 | |
| 15 namespace cc { | |
| 16 namespace { | |
| 17 | |
| 18 TEST(PicturePileImplTest, AnalyzeIsSolidUnscaled) { | |
| 19 gfx::Size tile_size(100, 100); | |
| 20 gfx::Size layer_bounds(400, 400); | |
| 21 | |
| 22 scoped_ptr<FakePicturePile> recording_source = | |
| 23 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 24 | |
| 25 SkPaint solid_paint; | |
| 26 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34); | |
| 27 solid_paint.setColor(solid_color); | |
| 28 | |
| 29 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67); | |
| 30 SkPaint non_solid_paint; | |
| 31 non_solid_paint.setColor(non_solid_color); | |
| 32 | |
| 33 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), | |
| 34 solid_paint); | |
| 35 recording_source->Rerecord(); | |
| 36 | |
| 37 scoped_refptr<FakePicturePileImpl> pile = | |
| 38 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 39 | |
| 40 // Ensure everything is solid. | |
| 41 for (int y = 0; y <= 300; y += 100) { | |
| 42 for (int x = 0; x <= 300; x += 100) { | |
| 43 RasterSource::SolidColorAnalysis analysis; | |
| 44 gfx::Rect rect(x, y, 100, 100); | |
| 45 pile->PerformSolidColorAnalysis(rect, 1.0, &analysis); | |
| 46 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString(); | |
| 47 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString(); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 // Add one non-solid pixel and recreate the raster source. | |
| 52 recording_source->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), | |
| 53 non_solid_paint); | |
| 54 recording_source->Rerecord(); | |
| 55 pile = FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 56 | |
| 57 RasterSource::SolidColorAnalysis analysis; | |
| 58 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 100, 100), 1.0, &analysis); | |
| 59 EXPECT_FALSE(analysis.is_solid_color); | |
| 60 | |
| 61 pile->PerformSolidColorAnalysis(gfx::Rect(100, 0, 100, 100), 1.0, &analysis); | |
| 62 EXPECT_TRUE(analysis.is_solid_color); | |
| 63 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 64 | |
| 65 // Boundaries should be clipped. | |
| 66 analysis.is_solid_color = false; | |
| 67 pile->PerformSolidColorAnalysis(gfx::Rect(350, 0, 100, 100), 1.0, &analysis); | |
| 68 EXPECT_TRUE(analysis.is_solid_color); | |
| 69 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 70 | |
| 71 analysis.is_solid_color = false; | |
| 72 pile->PerformSolidColorAnalysis(gfx::Rect(0, 350, 100, 100), 1.0, &analysis); | |
| 73 EXPECT_TRUE(analysis.is_solid_color); | |
| 74 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 75 | |
| 76 analysis.is_solid_color = false; | |
| 77 pile->PerformSolidColorAnalysis(gfx::Rect(350, 350, 100, 100), 1.0, | |
| 78 &analysis); | |
| 79 EXPECT_TRUE(analysis.is_solid_color); | |
| 80 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 81 } | |
| 82 | |
| 83 TEST(PicturePileImplTest, AnalyzeIsSolidScaled) { | |
| 84 gfx::Size tile_size(100, 100); | |
| 85 gfx::Size layer_bounds(400, 400); | |
| 86 | |
| 87 scoped_ptr<FakePicturePile> recording_source = | |
| 88 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 89 | |
| 90 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34); | |
| 91 SkPaint solid_paint; | |
| 92 solid_paint.setColor(solid_color); | |
| 93 | |
| 94 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67); | |
| 95 SkPaint non_solid_paint; | |
| 96 non_solid_paint.setColor(non_solid_color); | |
| 97 | |
| 98 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 400, 400), | |
| 99 solid_paint); | |
| 100 recording_source->Rerecord(); | |
| 101 | |
| 102 scoped_refptr<FakePicturePileImpl> pile = | |
| 103 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 104 | |
| 105 // Ensure everything is solid. | |
| 106 for (int y = 0; y <= 30; y += 10) { | |
| 107 for (int x = 0; x <= 30; x += 10) { | |
| 108 RasterSource::SolidColorAnalysis analysis; | |
| 109 gfx::Rect rect(x, y, 10, 10); | |
| 110 pile->PerformSolidColorAnalysis(rect, 0.1f, &analysis); | |
| 111 EXPECT_TRUE(analysis.is_solid_color) << rect.ToString(); | |
| 112 EXPECT_EQ(analysis.solid_color, solid_color) << rect.ToString(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 // Add one non-solid pixel and recreate the raster source. | |
| 117 recording_source->add_draw_rect_with_paint(gfx::Rect(50, 50, 1, 1), | |
| 118 non_solid_paint); | |
| 119 recording_source->Rerecord(); | |
| 120 pile = FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 121 | |
| 122 RasterSource::SolidColorAnalysis analysis; | |
| 123 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 10, 10), 0.1f, &analysis); | |
| 124 EXPECT_FALSE(analysis.is_solid_color); | |
| 125 | |
| 126 pile->PerformSolidColorAnalysis(gfx::Rect(10, 0, 10, 10), 0.1f, &analysis); | |
| 127 EXPECT_TRUE(analysis.is_solid_color); | |
| 128 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 129 | |
| 130 // Boundaries should be clipped. | |
| 131 analysis.is_solid_color = false; | |
| 132 pile->PerformSolidColorAnalysis(gfx::Rect(35, 0, 10, 10), 0.1f, &analysis); | |
| 133 EXPECT_TRUE(analysis.is_solid_color); | |
| 134 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 135 | |
| 136 analysis.is_solid_color = false; | |
| 137 pile->PerformSolidColorAnalysis(gfx::Rect(0, 35, 10, 10), 0.1f, &analysis); | |
| 138 EXPECT_TRUE(analysis.is_solid_color); | |
| 139 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 140 | |
| 141 analysis.is_solid_color = false; | |
| 142 pile->PerformSolidColorAnalysis(gfx::Rect(35, 35, 10, 10), 0.1f, &analysis); | |
| 143 EXPECT_TRUE(analysis.is_solid_color); | |
| 144 EXPECT_EQ(analysis.solid_color, solid_color); | |
| 145 } | |
| 146 | |
| 147 TEST(PicturePileImplTest, AnalyzeIsSolidEmpty) { | |
| 148 gfx::Size tile_size(100, 100); | |
| 149 gfx::Size layer_bounds(400, 400); | |
| 150 | |
| 151 scoped_refptr<FakePicturePileImpl> pile = | |
| 152 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); | |
| 153 RasterSource::SolidColorAnalysis analysis; | |
| 154 EXPECT_FALSE(analysis.is_solid_color); | |
| 155 | |
| 156 pile->PerformSolidColorAnalysis(gfx::Rect(0, 0, 400, 400), 1.f, &analysis); | |
| 157 | |
| 158 EXPECT_TRUE(analysis.is_solid_color); | |
| 159 EXPECT_EQ(analysis.solid_color, SkColorSetARGB(0, 0, 0, 0)); | |
| 160 } | |
| 161 | |
| 162 TEST(PicturePileImplTest, PixelRefIteratorDiscardableRefsOneTile) { | |
| 163 gfx::Size tile_size(256, 256); | |
| 164 gfx::Size layer_bounds(512, 512); | |
| 165 | |
| 166 scoped_ptr<FakePicturePile> recording_source = | |
| 167 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 168 | |
| 169 SkBitmap discardable_bitmap[2][2]; | |
| 170 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][0]); | |
| 171 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[0][1]); | |
| 172 CreateBitmap(gfx::Size(32, 32), "discardable", &discardable_bitmap[1][1]); | |
| 173 | |
| 174 // Discardable pixel refs are found in the following cells: | |
| 175 // |---|---| | |
| 176 // | x | x | | |
| 177 // |---|---| | |
| 178 // | | x | | |
| 179 // |---|---| | |
| 180 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0)); | |
| 181 recording_source->add_draw_bitmap(discardable_bitmap[0][1], | |
| 182 gfx::Point(260, 0)); | |
| 183 recording_source->add_draw_bitmap(discardable_bitmap[1][1], | |
| 184 gfx::Point(260, 260)); | |
| 185 recording_source->SetGatherPixelRefs(true); | |
| 186 recording_source->Rerecord(); | |
| 187 | |
| 188 scoped_refptr<FakePicturePileImpl> pile = | |
| 189 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 190 | |
| 191 // Tile sized iterators. These should find only one pixel ref. | |
| 192 { | |
| 193 PicturePileImpl::PixelRefIterator iterator( | |
| 194 gfx::Rect(0, 0, 256, 256), 1.0, pile.get()); | |
| 195 EXPECT_TRUE(iterator); | |
| 196 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 197 EXPECT_FALSE(++iterator); | |
| 198 } | |
| 199 { | |
| 200 PicturePileImpl::PixelRefIterator iterator( | |
| 201 gfx::Rect(0, 0, 512, 512), 2.0, pile.get()); | |
| 202 EXPECT_TRUE(iterator); | |
| 203 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 204 EXPECT_FALSE(++iterator); | |
| 205 } | |
| 206 { | |
| 207 PicturePileImpl::PixelRefIterator iterator( | |
| 208 gfx::Rect(0, 0, 128, 128), 0.5, pile.get()); | |
| 209 EXPECT_TRUE(iterator); | |
| 210 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 211 EXPECT_FALSE(++iterator); | |
| 212 } | |
| 213 // Shifted tile sized iterators. These should find only one pixel ref. | |
| 214 { | |
| 215 PicturePileImpl::PixelRefIterator iterator( | |
| 216 gfx::Rect(260, 260, 256, 256), 1.0, pile.get()); | |
| 217 EXPECT_TRUE(iterator); | |
| 218 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 219 EXPECT_FALSE(++iterator); | |
| 220 } | |
| 221 { | |
| 222 PicturePileImpl::PixelRefIterator iterator( | |
| 223 gfx::Rect(520, 520, 512, 512), 2.0, pile.get()); | |
| 224 EXPECT_TRUE(iterator); | |
| 225 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 226 EXPECT_FALSE(++iterator); | |
| 227 } | |
| 228 { | |
| 229 PicturePileImpl::PixelRefIterator iterator( | |
| 230 gfx::Rect(130, 130, 128, 128), 0.5, pile.get()); | |
| 231 EXPECT_TRUE(iterator); | |
| 232 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 233 EXPECT_FALSE(++iterator); | |
| 234 } | |
| 235 // Ensure there's no discardable pixel refs in the empty cell | |
| 236 { | |
| 237 PicturePileImpl::PixelRefIterator iterator( | |
| 238 gfx::Rect(0, 256, 256, 256), 1.0, pile.get()); | |
| 239 EXPECT_FALSE(iterator); | |
| 240 } | |
| 241 // Layer sized iterators. These should find three pixel ref. | |
| 242 { | |
| 243 PicturePileImpl::PixelRefIterator iterator( | |
| 244 gfx::Rect(0, 0, 512, 512), 1.0, pile.get()); | |
| 245 EXPECT_TRUE(iterator); | |
| 246 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 247 EXPECT_TRUE(++iterator); | |
| 248 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef()); | |
| 249 EXPECT_TRUE(++iterator); | |
| 250 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 251 EXPECT_FALSE(++iterator); | |
| 252 } | |
| 253 { | |
| 254 PicturePileImpl::PixelRefIterator iterator( | |
| 255 gfx::Rect(0, 0, 1024, 1024), 2.0, pile.get()); | |
| 256 EXPECT_TRUE(iterator); | |
| 257 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 258 EXPECT_TRUE(++iterator); | |
| 259 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef()); | |
| 260 EXPECT_TRUE(++iterator); | |
| 261 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 262 EXPECT_FALSE(++iterator); | |
| 263 } | |
| 264 { | |
| 265 PicturePileImpl::PixelRefIterator iterator( | |
| 266 gfx::Rect(0, 0, 256, 256), 0.5, pile.get()); | |
| 267 EXPECT_TRUE(iterator); | |
| 268 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 269 EXPECT_TRUE(++iterator); | |
| 270 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef()); | |
| 271 EXPECT_TRUE(++iterator); | |
| 272 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 273 EXPECT_FALSE(++iterator); | |
| 274 } | |
| 275 | |
| 276 // Copy test. | |
| 277 PicturePileImpl::PixelRefIterator iterator( | |
| 278 gfx::Rect(0, 0, 512, 512), 1.0, pile.get()); | |
| 279 EXPECT_TRUE(iterator); | |
| 280 EXPECT_TRUE(*iterator == discardable_bitmap[0][0].pixelRef()); | |
| 281 EXPECT_TRUE(++iterator); | |
| 282 EXPECT_TRUE(*iterator == discardable_bitmap[0][1].pixelRef()); | |
| 283 | |
| 284 // copy now points to the same spot as iterator, | |
| 285 // but both can be incremented independently. | |
| 286 PicturePileImpl::PixelRefIterator copy = iterator; | |
| 287 EXPECT_TRUE(++iterator); | |
| 288 EXPECT_TRUE(*iterator == discardable_bitmap[1][1].pixelRef()); | |
| 289 EXPECT_FALSE(++iterator); | |
| 290 | |
| 291 EXPECT_TRUE(copy); | |
| 292 EXPECT_TRUE(*copy == discardable_bitmap[0][1].pixelRef()); | |
| 293 EXPECT_TRUE(++copy); | |
| 294 EXPECT_TRUE(*copy == discardable_bitmap[1][1].pixelRef()); | |
| 295 EXPECT_FALSE(++copy); | |
| 296 } | |
| 297 | |
| 298 TEST(PicturePileImplTest, RasterFullContents) { | |
| 299 gfx::Size tile_size(1000, 1000); | |
| 300 gfx::Size layer_bounds(3, 5); | |
| 301 float contents_scale = 1.5f; | |
| 302 float raster_divisions = 2.f; | |
| 303 | |
| 304 scoped_ptr<FakePicturePile> recording_source = | |
| 305 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 306 recording_source->SetBackgroundColor(SK_ColorBLACK); | |
| 307 recording_source->SetIsSolidColor(false); | |
| 308 recording_source->SetRequiresClear(false); | |
| 309 recording_source->SetClearCanvasWithDebugColor(false); | |
| 310 | |
| 311 // Because the caller sets content opaque, it also promises that it | |
| 312 // has at least filled in layer_bounds opaquely. | |
| 313 SkPaint white_paint; | |
| 314 white_paint.setColor(SK_ColorWHITE); | |
| 315 recording_source->add_draw_rect_with_paint(gfx::Rect(layer_bounds), | |
| 316 white_paint); | |
| 317 | |
| 318 recording_source->SetMinContentsScale(contents_scale); | |
| 319 recording_source->Rerecord(); | |
| 320 | |
| 321 scoped_refptr<FakePicturePileImpl> pile = | |
| 322 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 323 | |
| 324 gfx::Size content_bounds( | |
| 325 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale))); | |
| 326 | |
| 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)); | |
| 336 | |
| 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); | |
| 342 | |
| 343 SkBitmap bitmap; | |
| 344 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height()); | |
| 345 SkCanvas canvas(bitmap); | |
| 346 canvas.clear(SK_ColorTRANSPARENT); | |
| 347 | |
| 348 pile->PlaybackToCanvas(&canvas, canvas_rect, contents_scale); | |
| 349 | |
| 350 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 351 int num_pixels = bitmap.width() * bitmap.height(); | |
| 352 bool all_white = true; | |
| 353 for (int i = 0; i < num_pixels; ++i) { | |
| 354 EXPECT_EQ(SkColorGetA(pixels[i]), 255u); | |
| 355 all_white &= (SkColorGetR(pixels[i]) == 255); | |
| 356 all_white &= (SkColorGetG(pixels[i]) == 255); | |
| 357 all_white &= (SkColorGetB(pixels[i]) == 255); | |
| 358 } | |
| 359 | |
| 360 // If the canvas doesn't extend past the edge of the content, | |
| 361 // it should be entirely white. Otherwise, the edge of the content | |
| 362 // will be non-white. | |
| 363 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect)); | |
| 364 } | |
| 365 } | |
| 366 } | |
| 367 | |
| 368 TEST(PicturePileImpl, RasterContentsTransparent) { | |
| 369 gfx::Size tile_size(1000, 1000); | |
| 370 gfx::Size layer_bounds(5, 3); | |
| 371 float contents_scale = 0.5f; | |
| 372 | |
| 373 scoped_ptr<FakePicturePile> recording_source = | |
| 374 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 375 recording_source->SetBackgroundColor(SK_ColorTRANSPARENT); | |
| 376 recording_source->SetRequiresClear(true); | |
| 377 recording_source->SetMinContentsScale(contents_scale); | |
| 378 recording_source->SetClearCanvasWithDebugColor(false); | |
| 379 recording_source->Rerecord(); | |
| 380 | |
| 381 scoped_refptr<FakePicturePileImpl> pile = | |
| 382 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 383 gfx::Size content_bounds( | |
| 384 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale))); | |
| 385 | |
| 386 gfx::Rect canvas_rect(content_bounds); | |
| 387 canvas_rect.Inset(0, 0, -1, -1); | |
| 388 | |
| 389 SkBitmap bitmap; | |
| 390 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height()); | |
| 391 SkCanvas canvas(bitmap); | |
| 392 | |
| 393 pile->PlaybackToCanvas(&canvas, canvas_rect, contents_scale); | |
| 394 | |
| 395 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 396 int num_pixels = bitmap.width() * bitmap.height(); | |
| 397 for (int i = 0; i < num_pixels; ++i) { | |
| 398 EXPECT_EQ(SkColorGetA(pixels[i]), 0u); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 class OverlapTest : public ::testing::TestWithParam<float> { | |
| 403 public: | |
| 404 static float MinContentsScale() { return 1.f / 4.f; } | |
| 405 }; | |
| 406 | |
| 407 TEST_P(OverlapTest, NoOverlap) { | |
| 408 gfx::Size tile_size(10, 10); | |
| 409 gfx::Size layer_bounds(30, 30); | |
| 410 gfx::Size bigger_than_layer_bounds(300, 300); | |
| 411 float contents_scale = GetParam(); | |
| 412 // Pick an opaque color to not have to deal with premultiplication off-by-one. | |
| 413 SkColor test_color = SkColorSetARGB(255, 45, 56, 67); | |
| 414 | |
| 415 scoped_ptr<FakePicturePile> recording_source = | |
| 416 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 417 recording_source->SetBackgroundColor(SK_ColorTRANSPARENT); | |
| 418 recording_source->SetRequiresClear(true); | |
| 419 recording_source->SetMinContentsScale(MinContentsScale()); | |
| 420 recording_source->SetClearCanvasWithDebugColor(true); | |
| 421 | |
| 422 SkPaint color_paint; | |
| 423 color_paint.setColor(test_color); | |
| 424 // Additive paint, so that if two paints overlap, the color will change. | |
| 425 color_paint.setXfermodeMode(SkXfermode::kPlus_Mode); | |
| 426 // Paint outside the layer to make sure that blending works. | |
| 427 recording_source->add_draw_rect_with_paint( | |
| 428 gfx::RectF(bigger_than_layer_bounds), color_paint); | |
| 429 recording_source->Rerecord(); | |
| 430 | |
| 431 scoped_refptr<FakePicturePileImpl> pile = | |
| 432 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 433 gfx::Size content_bounds( | |
| 434 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale))); | |
| 435 | |
| 436 SkBitmap bitmap; | |
| 437 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height()); | |
| 438 SkCanvas canvas(bitmap); | |
| 439 | |
| 440 pile->PlaybackToCanvas(&canvas, gfx::Rect(content_bounds), contents_scale); | |
| 441 | |
| 442 for (int y = 0; y < bitmap.height(); y++) { | |
| 443 for (int x = 0; x < bitmap.width(); x++) { | |
| 444 SkColor color = bitmap.getColor(x, y); | |
| 445 EXPECT_EQ(SkColorGetR(test_color), SkColorGetR(color)) << "x: " << x | |
| 446 << ", y: " << y; | |
| 447 EXPECT_EQ(SkColorGetG(test_color), SkColorGetG(color)) << "x: " << x | |
| 448 << ", y: " << y; | |
| 449 EXPECT_EQ(SkColorGetB(test_color), SkColorGetB(color)) << "x: " << x | |
| 450 << ", y: " << y; | |
| 451 EXPECT_EQ(SkColorGetA(test_color), SkColorGetA(color)) << "x: " << x | |
| 452 << ", y: " << y; | |
| 453 if (test_color != color) | |
| 454 break; | |
| 455 } | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 INSTANTIATE_TEST_CASE_P(PicturePileImpl, | |
| 460 OverlapTest, | |
| 461 ::testing::Values(1.f, 0.873f, 1.f / 4.f, 4.f)); | |
| 462 | |
| 463 TEST(PicturePileImplTest, PixelRefIteratorBorders) { | |
| 464 // 3 tile width / 1 tile height pile | |
| 465 gfx::Size tile_size(128, 128); | |
| 466 gfx::Size layer_bounds(320, 128); | |
| 467 | |
| 468 // Fake picture pile uses a tile grid the size of the tile. So, | |
| 469 // any iteration that intersects with a tile will return all pixel refs | |
| 470 // inside of it. | |
| 471 scoped_ptr<FakePicturePile> recording_source = | |
| 472 FakePicturePile::CreateFilledPile(tile_size, layer_bounds); | |
| 473 recording_source->SetMinContentsScale(0.5f); | |
| 474 | |
| 475 // Bitmaps 0-2 are exactly on tiles 0-2, so that they overlap the borders | |
| 476 // of adjacent tiles. | |
| 477 gfx::Rect bitmap_rects[] = { | |
| 478 recording_source->tiling().TileBounds(0, 0), | |
| 479 recording_source->tiling().TileBounds(1, 0), | |
| 480 recording_source->tiling().TileBounds(2, 0), | |
| 481 }; | |
| 482 SkBitmap discardable_bitmap[arraysize(bitmap_rects)]; | |
| 483 | |
| 484 for (size_t i = 0; i < arraysize(bitmap_rects); ++i) { | |
| 485 CreateBitmap(bitmap_rects[i].size(), "discardable", &discardable_bitmap[i]); | |
| 486 recording_source->add_draw_bitmap(discardable_bitmap[i], | |
| 487 bitmap_rects[i].origin()); | |
| 488 } | |
| 489 | |
| 490 recording_source->SetGatherPixelRefs(true); | |
| 491 recording_source->Rerecord(); | |
| 492 | |
| 493 scoped_refptr<FakePicturePileImpl> pile = | |
| 494 FakePicturePileImpl::CreateFromPile(recording_source.get(), nullptr); | |
| 495 | |
| 496 // Sanity check that bitmaps 0-2 intersect the borders of their adjacent | |
| 497 // tiles, but not the actual tiles. | |
| 498 EXPECT_TRUE( | |
| 499 bitmap_rects[0].Intersects(pile->tiling().TileBoundsWithBorder(1, 0))); | |
| 500 EXPECT_FALSE(bitmap_rects[0].Intersects(pile->tiling().TileBounds(1, 0))); | |
| 501 EXPECT_TRUE( | |
| 502 bitmap_rects[1].Intersects(pile->tiling().TileBoundsWithBorder(0, 0))); | |
| 503 EXPECT_FALSE(bitmap_rects[1].Intersects(pile->tiling().TileBounds(0, 0))); | |
| 504 EXPECT_TRUE( | |
| 505 bitmap_rects[1].Intersects(pile->tiling().TileBoundsWithBorder(2, 0))); | |
| 506 EXPECT_FALSE(bitmap_rects[1].Intersects(pile->tiling().TileBounds(2, 0))); | |
| 507 EXPECT_TRUE( | |
| 508 bitmap_rects[2].Intersects(pile->tiling().TileBoundsWithBorder(1, 0))); | |
| 509 EXPECT_FALSE(bitmap_rects[2].Intersects(pile->tiling().TileBounds(1, 0))); | |
| 510 | |
| 511 // Tile-sized iterators. | |
| 512 { | |
| 513 // Because tile 0's borders extend onto tile 1, it will include both | |
| 514 // bitmap 0 and 1. However, it should *not* include bitmap 2. | |
| 515 PicturePileImpl::PixelRefIterator iterator( | |
| 516 pile->tiling().TileBounds(0, 0), 1.f, pile.get()); | |
| 517 EXPECT_TRUE(iterator); | |
| 518 EXPECT_TRUE(*iterator == discardable_bitmap[0].pixelRef()); | |
| 519 EXPECT_TRUE(++iterator); | |
| 520 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef()); | |
| 521 EXPECT_FALSE(++iterator); | |
| 522 } | |
| 523 { | |
| 524 // Tile 1 + borders hits all bitmaps. | |
| 525 PicturePileImpl::PixelRefIterator iterator( | |
| 526 pile->tiling().TileBounds(1, 0), 1.f, pile.get()); | |
| 527 EXPECT_TRUE(iterator); | |
| 528 EXPECT_TRUE(*iterator == discardable_bitmap[0].pixelRef()); | |
| 529 EXPECT_TRUE(++iterator); | |
| 530 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef()); | |
| 531 EXPECT_TRUE(++iterator); | |
| 532 EXPECT_TRUE(*iterator == discardable_bitmap[2].pixelRef()); | |
| 533 EXPECT_FALSE(++iterator); | |
| 534 } | |
| 535 { | |
| 536 // Tile 2 should not include bitmap 0, which is only on tile 0 and the | |
| 537 // borders of tile 1. | |
| 538 PicturePileImpl::PixelRefIterator iterator( | |
| 539 pile->tiling().TileBounds(2, 0), 1.f, pile.get()); | |
| 540 EXPECT_TRUE(iterator); | |
| 541 EXPECT_TRUE(*iterator == discardable_bitmap[1].pixelRef()); | |
| 542 EXPECT_TRUE(++iterator); | |
| 543 EXPECT_TRUE(*iterator == discardable_bitmap[2].pixelRef()); | |
| 544 EXPECT_FALSE(++iterator); | |
| 545 } | |
| 546 } | |
| 547 | |
| 548 } // namespace | |
| 549 } // namespace cc | |
| OLD | NEW |