| 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 "cc/playback/raster_source.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "cc/test/fake_recording_source.h" | |
| 12 #include "cc/test/skia_common.h" | |
| 13 #include "cc/tiles/software_image_decode_cache.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "third_party/skia/include/core/SkPixelRef.h" | |
| 16 #include "third_party/skia/include/core/SkRefCnt.h" | |
| 17 #include "third_party/skia/include/core/SkShader.h" | |
| 18 #include "ui/gfx/geometry/rect.h" | |
| 19 #include "ui/gfx/geometry/size_conversions.h" | |
| 20 | |
| 21 namespace cc { | |
| 22 namespace { | |
| 23 | |
| 24 TEST(RasterSourceTest, AnalyzeIsSolidUnscaled) { | |
| 25 gfx::Size layer_bounds(400, 400); | |
| 26 | |
| 27 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 28 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 29 | |
| 30 PaintFlags solid_flags; | |
| 31 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34); | |
| 32 solid_flags.setColor(solid_color); | |
| 33 | |
| 34 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67); | |
| 35 SkColor color = SK_ColorTRANSPARENT; | |
| 36 PaintFlags non_solid_flags; | |
| 37 bool is_solid_color = false; | |
| 38 non_solid_flags.setColor(non_solid_color); | |
| 39 | |
| 40 recording_source->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 41 solid_flags); | |
| 42 recording_source->Rerecord(); | |
| 43 | |
| 44 scoped_refptr<RasterSource> raster = | |
| 45 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 46 | |
| 47 // Ensure everything is solid. | |
| 48 for (int y = 0; y <= 300; y += 100) { | |
| 49 for (int x = 0; x <= 300; x += 100) { | |
| 50 gfx::Rect rect(x, y, 100, 100); | |
| 51 is_solid_color = raster->PerformSolidColorAnalysis(rect, 1.f, &color); | |
| 52 EXPECT_TRUE(is_solid_color) << rect.ToString(); | |
| 53 EXPECT_EQ(solid_color, color) << rect.ToString(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 // Add one non-solid pixel and recreate the raster source. | |
| 58 recording_source->add_draw_rect_with_flags(gfx::Rect(50, 50, 1, 1), | |
| 59 non_solid_flags); | |
| 60 recording_source->Rerecord(); | |
| 61 raster = | |
| 62 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 63 | |
| 64 color = SK_ColorTRANSPARENT; | |
| 65 is_solid_color = | |
| 66 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 100, 100), 1.f, &color); | |
| 67 EXPECT_FALSE(is_solid_color); | |
| 68 | |
| 69 color = SK_ColorTRANSPARENT; | |
| 70 is_solid_color = raster->PerformSolidColorAnalysis( | |
| 71 gfx::Rect(100, 0, 100, 100), 1.f, &color); | |
| 72 EXPECT_TRUE(is_solid_color); | |
| 73 EXPECT_EQ(solid_color, color); | |
| 74 | |
| 75 // Boundaries should be clipped. | |
| 76 color = SK_ColorTRANSPARENT; | |
| 77 is_solid_color = raster->PerformSolidColorAnalysis( | |
| 78 gfx::Rect(350, 0, 100, 100), 1.f, &color); | |
| 79 EXPECT_TRUE(is_solid_color); | |
| 80 EXPECT_EQ(solid_color, color); | |
| 81 | |
| 82 color = SK_ColorTRANSPARENT; | |
| 83 is_solid_color = raster->PerformSolidColorAnalysis( | |
| 84 gfx::Rect(0, 350, 100, 100), 1.f, &color); | |
| 85 EXPECT_TRUE(is_solid_color); | |
| 86 EXPECT_EQ(solid_color, color); | |
| 87 | |
| 88 color = SK_ColorTRANSPARENT; | |
| 89 is_solid_color = raster->PerformSolidColorAnalysis( | |
| 90 gfx::Rect(350, 350, 100, 100), 1.f, &color); | |
| 91 EXPECT_TRUE(is_solid_color); | |
| 92 EXPECT_EQ(solid_color, color); | |
| 93 } | |
| 94 | |
| 95 TEST(RasterSourceTest, AnalyzeIsSolidScaled) { | |
| 96 gfx::Size layer_bounds(400, 400); | |
| 97 | |
| 98 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 99 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 100 | |
| 101 SkColor solid_color = SkColorSetARGB(255, 12, 23, 34); | |
| 102 SkColor color = SK_ColorTRANSPARENT; | |
| 103 PaintFlags solid_flags; | |
| 104 bool is_solid_color = false; | |
| 105 solid_flags.setColor(solid_color); | |
| 106 | |
| 107 SkColor non_solid_color = SkColorSetARGB(128, 45, 56, 67); | |
| 108 PaintFlags non_solid_flags; | |
| 109 non_solid_flags.setColor(non_solid_color); | |
| 110 | |
| 111 recording_source->add_draw_rect_with_flags(gfx::Rect(0, 0, 400, 400), | |
| 112 solid_flags); | |
| 113 recording_source->Rerecord(); | |
| 114 | |
| 115 scoped_refptr<RasterSource> raster = | |
| 116 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 117 | |
| 118 // Ensure everything is solid. | |
| 119 for (int y = 0; y <= 30; y += 10) { | |
| 120 for (int x = 0; x <= 30; x += 10) { | |
| 121 gfx::Rect rect(x, y, 10, 10); | |
| 122 is_solid_color = raster->PerformSolidColorAnalysis(rect, 0.1f, &color); | |
| 123 EXPECT_TRUE(is_solid_color) << rect.ToString(); | |
| 124 EXPECT_EQ(color, solid_color) << rect.ToString(); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // Add one non-solid pixel and recreate the raster source. | |
| 129 recording_source->add_draw_rect_with_flags(gfx::Rect(50, 50, 1, 1), | |
| 130 non_solid_flags); | |
| 131 recording_source->Rerecord(); | |
| 132 raster = | |
| 133 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 134 | |
| 135 color = SK_ColorTRANSPARENT; | |
| 136 is_solid_color = | |
| 137 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 10, 10), 0.1f, &color); | |
| 138 EXPECT_FALSE(is_solid_color); | |
| 139 | |
| 140 color = SK_ColorTRANSPARENT; | |
| 141 is_solid_color = | |
| 142 raster->PerformSolidColorAnalysis(gfx::Rect(10, 0, 10, 10), 0.1f, &color); | |
| 143 EXPECT_TRUE(is_solid_color); | |
| 144 EXPECT_EQ(color, solid_color); | |
| 145 | |
| 146 // Boundaries should be clipped. | |
| 147 color = SK_ColorTRANSPARENT; | |
| 148 is_solid_color = | |
| 149 raster->PerformSolidColorAnalysis(gfx::Rect(35, 0, 10, 10), 0.1f, &color); | |
| 150 EXPECT_TRUE(is_solid_color); | |
| 151 EXPECT_EQ(color, solid_color); | |
| 152 | |
| 153 color = SK_ColorTRANSPARENT; | |
| 154 is_solid_color = | |
| 155 raster->PerformSolidColorAnalysis(gfx::Rect(0, 35, 10, 10), 0.1f, &color); | |
| 156 EXPECT_TRUE(is_solid_color); | |
| 157 EXPECT_EQ(color, solid_color); | |
| 158 | |
| 159 color = SK_ColorTRANSPARENT; | |
| 160 is_solid_color = raster->PerformSolidColorAnalysis(gfx::Rect(35, 35, 10, 10), | |
| 161 0.1f, &color); | |
| 162 EXPECT_TRUE(is_solid_color); | |
| 163 EXPECT_EQ(color, solid_color); | |
| 164 } | |
| 165 | |
| 166 TEST(RasterSourceTest, AnalyzeIsSolidEmpty) { | |
| 167 gfx::Size layer_bounds(400, 400); | |
| 168 | |
| 169 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 170 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 171 recording_source->Rerecord(); | |
| 172 | |
| 173 scoped_refptr<RasterSource> raster = | |
| 174 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 175 | |
| 176 SkColor color = SK_ColorTRANSPARENT; | |
| 177 bool is_solid_color = | |
| 178 raster->PerformSolidColorAnalysis(gfx::Rect(0, 0, 400, 400), 1.f, &color); | |
| 179 | |
| 180 EXPECT_TRUE(is_solid_color); | |
| 181 EXPECT_EQ(color, SkColorSetARGB(0, 0, 0, 0)); | |
| 182 } | |
| 183 | |
| 184 TEST(RasterSourceTest, PixelRefIteratorDiscardableRefsOneTile) { | |
| 185 gfx::Size layer_bounds(512, 512); | |
| 186 | |
| 187 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 188 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 189 | |
| 190 sk_sp<SkImage> discardable_image[2][2]; | |
| 191 discardable_image[0][0] = CreateDiscardableImage(gfx::Size(32, 32)); | |
| 192 discardable_image[0][1] = CreateDiscardableImage(gfx::Size(32, 32)); | |
| 193 discardable_image[1][1] = CreateDiscardableImage(gfx::Size(32, 32)); | |
| 194 | |
| 195 // Discardable pixel refs are found in the following cells: | |
| 196 // |---|---| | |
| 197 // | x | x | | |
| 198 // |---|---| | |
| 199 // | | x | | |
| 200 // |---|---| | |
| 201 recording_source->add_draw_image(discardable_image[0][0], gfx::Point(0, 0)); | |
| 202 recording_source->add_draw_image(discardable_image[0][1], gfx::Point(260, 0)); | |
| 203 recording_source->add_draw_image(discardable_image[1][1], | |
| 204 gfx::Point(260, 260)); | |
| 205 recording_source->SetGenerateDiscardableImagesMetadata(true); | |
| 206 recording_source->Rerecord(); | |
| 207 | |
| 208 scoped_refptr<RasterSource> raster = | |
| 209 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 210 | |
| 211 // Tile sized iterators. These should find only one pixel ref. | |
| 212 { | |
| 213 std::vector<DrawImage> images; | |
| 214 raster->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256), 1.f, &images); | |
| 215 EXPECT_EQ(1u, images.size()); | |
| 216 EXPECT_EQ(discardable_image[0][0], images[0].image()); | |
| 217 } | |
| 218 // Shifted tile sized iterators. These should find only one pixel ref. | |
| 219 { | |
| 220 std::vector<DrawImage> images; | |
| 221 raster->GetDiscardableImagesInRect(gfx::Rect(260, 260, 256, 256), 1.f, | |
| 222 &images); | |
| 223 EXPECT_EQ(1u, images.size()); | |
| 224 EXPECT_EQ(discardable_image[1][1], images[0].image()); | |
| 225 } | |
| 226 // Ensure there's no discardable pixel refs in the empty cell | |
| 227 { | |
| 228 std::vector<DrawImage> images; | |
| 229 raster->GetDiscardableImagesInRect(gfx::Rect(0, 256, 256, 256), 1.f, | |
| 230 &images); | |
| 231 EXPECT_EQ(0u, images.size()); | |
| 232 } | |
| 233 // Layer sized iterators. These should find three pixel ref. | |
| 234 { | |
| 235 std::vector<DrawImage> images; | |
| 236 raster->GetDiscardableImagesInRect(gfx::Rect(0, 0, 512, 512), 1.f, &images); | |
| 237 EXPECT_EQ(3u, images.size()); | |
| 238 EXPECT_EQ(discardable_image[0][0], images[0].image()); | |
| 239 EXPECT_EQ(discardable_image[0][1], images[1].image()); | |
| 240 EXPECT_EQ(discardable_image[1][1], images[2].image()); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 TEST(RasterSourceTest, RasterFullContents) { | |
| 245 gfx::Size layer_bounds(3, 5); | |
| 246 float contents_scale = 1.5f; | |
| 247 float raster_divisions = 2.f; | |
| 248 | |
| 249 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 250 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 251 recording_source->SetBackgroundColor(SK_ColorBLACK); | |
| 252 recording_source->SetClearCanvasWithDebugColor(false); | |
| 253 | |
| 254 // Because the caller sets content opaque, it also promises that it | |
| 255 // has at least filled in layer_bounds opaquely. | |
| 256 PaintFlags white_flags; | |
| 257 white_flags.setColor(SK_ColorWHITE); | |
| 258 recording_source->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 259 white_flags); | |
| 260 recording_source->Rerecord(); | |
| 261 | |
| 262 scoped_refptr<RasterSource> raster = | |
| 263 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 264 | |
| 265 gfx::Size content_bounds( | |
| 266 gfx::ScaleToCeiledSize(layer_bounds, contents_scale)); | |
| 267 | |
| 268 // Simulate drawing into different tiles at different offsets. | |
| 269 int step_x = std::ceil(content_bounds.width() / raster_divisions); | |
| 270 int step_y = std::ceil(content_bounds.height() / raster_divisions); | |
| 271 for (int offset_x = 0; offset_x < content_bounds.width(); | |
| 272 offset_x += step_x) { | |
| 273 for (int offset_y = 0; offset_y < content_bounds.height(); | |
| 274 offset_y += step_y) { | |
| 275 gfx::Rect content_rect(offset_x, offset_y, step_x, step_y); | |
| 276 content_rect.Intersect(gfx::Rect(content_bounds)); | |
| 277 | |
| 278 // Simulate a canvas rect larger than the content rect. Every pixel | |
| 279 // up to one pixel outside the content rect is guaranteed to be opaque. | |
| 280 // Outside of that is undefined. | |
| 281 gfx::Rect canvas_rect(content_rect); | |
| 282 canvas_rect.Inset(0, 0, -1, -1); | |
| 283 | |
| 284 SkBitmap bitmap; | |
| 285 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height()); | |
| 286 SkCanvas canvas(bitmap); | |
| 287 canvas.clear(SK_ColorTRANSPARENT); | |
| 288 | |
| 289 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect, | |
| 290 contents_scale, | |
| 291 RasterSource::PlaybackSettings()); | |
| 292 | |
| 293 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 294 int num_pixels = bitmap.width() * bitmap.height(); | |
| 295 bool all_white = true; | |
| 296 for (int i = 0; i < num_pixels; ++i) { | |
| 297 EXPECT_EQ(SkColorGetA(pixels[i]), 255u); | |
| 298 all_white &= (SkColorGetR(pixels[i]) == 255); | |
| 299 all_white &= (SkColorGetG(pixels[i]) == 255); | |
| 300 all_white &= (SkColorGetB(pixels[i]) == 255); | |
| 301 } | |
| 302 | |
| 303 // If the canvas doesn't extend past the edge of the content, | |
| 304 // it should be entirely white. Otherwise, the edge of the content | |
| 305 // will be non-white. | |
| 306 EXPECT_EQ(all_white, gfx::Rect(content_bounds).Contains(canvas_rect)); | |
| 307 } | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 TEST(RasterSourceTest, RasterPartialContents) { | |
| 312 gfx::Size layer_bounds(3, 5); | |
| 313 float contents_scale = 1.5f; | |
| 314 | |
| 315 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 316 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 317 recording_source->SetBackgroundColor(SK_ColorGREEN); | |
| 318 recording_source->SetClearCanvasWithDebugColor(false); | |
| 319 | |
| 320 // First record everything as white. | |
| 321 PaintFlags white_flags; | |
| 322 white_flags.setColor(SK_ColorWHITE); | |
| 323 recording_source->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 324 white_flags); | |
| 325 recording_source->Rerecord(); | |
| 326 | |
| 327 scoped_refptr<RasterSource> raster = | |
| 328 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 329 | |
| 330 gfx::Size content_bounds( | |
| 331 gfx::ScaleToCeiledSize(layer_bounds, contents_scale)); | |
| 332 | |
| 333 SkBitmap bitmap; | |
| 334 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height()); | |
| 335 SkCanvas canvas(bitmap); | |
| 336 canvas.clear(SK_ColorTRANSPARENT); | |
| 337 | |
| 338 // Playback the full rect which should make everything white. | |
| 339 gfx::Rect raster_full_rect(content_bounds); | |
| 340 gfx::Rect playback_rect(content_bounds); | |
| 341 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect, | |
| 342 contents_scale, RasterSource::PlaybackSettings()); | |
| 343 | |
| 344 { | |
| 345 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 346 for (int i = 0; i < bitmap.width(); ++i) { | |
| 347 for (int j = 0; j < bitmap.height(); ++j) { | |
| 348 SCOPED_TRACE(i); | |
| 349 SCOPED_TRACE(j); | |
| 350 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()])); | |
| 351 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()])); | |
| 352 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()])); | |
| 353 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()])); | |
| 354 } | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 // Re-record everything as black. | |
| 359 PaintFlags black_flags; | |
| 360 black_flags.setColor(SK_ColorBLACK); | |
| 361 recording_source->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 362 black_flags); | |
| 363 recording_source->Rerecord(); | |
| 364 | |
| 365 // Make a new RasterSource from the new recording. | |
| 366 raster = | |
| 367 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 368 | |
| 369 // We're going to playback from "everything is black" into a smaller area, | |
| 370 // that touches the edge pixels of the recording. | |
| 371 playback_rect.Inset(1, 2, 0, 1); | |
| 372 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect, | |
| 373 contents_scale, RasterSource::PlaybackSettings()); | |
| 374 | |
| 375 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 376 int num_black = 0; | |
| 377 int num_white = 0; | |
| 378 for (int i = 0; i < bitmap.width(); ++i) { | |
| 379 for (int j = 0; j < bitmap.height(); ++j) { | |
| 380 SCOPED_TRACE(j); | |
| 381 SCOPED_TRACE(i); | |
| 382 bool expect_black = playback_rect.Contains(i, j); | |
| 383 if (expect_black) { | |
| 384 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()])); | |
| 385 EXPECT_EQ(0u, SkColorGetR(pixels[i + j * bitmap.width()])); | |
| 386 EXPECT_EQ(0u, SkColorGetG(pixels[i + j * bitmap.width()])); | |
| 387 EXPECT_EQ(0u, SkColorGetB(pixels[i + j * bitmap.width()])); | |
| 388 ++num_black; | |
| 389 } else { | |
| 390 EXPECT_EQ(255u, SkColorGetA(pixels[i + j * bitmap.width()])); | |
| 391 EXPECT_EQ(255u, SkColorGetR(pixels[i + j * bitmap.width()])); | |
| 392 EXPECT_EQ(255u, SkColorGetG(pixels[i + j * bitmap.width()])); | |
| 393 EXPECT_EQ(255u, SkColorGetB(pixels[i + j * bitmap.width()])); | |
| 394 ++num_white; | |
| 395 } | |
| 396 } | |
| 397 } | |
| 398 EXPECT_GT(num_black, 0); | |
| 399 EXPECT_GT(num_white, 0); | |
| 400 } | |
| 401 | |
| 402 TEST(RasterSourceTest, RasterPartialClear) { | |
| 403 gfx::Size layer_bounds(3, 5); | |
| 404 gfx::Size partial_bounds(2, 4); | |
| 405 float contents_scale = 1.5f; | |
| 406 | |
| 407 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 408 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 409 recording_source->SetBackgroundColor(SK_ColorGREEN); | |
| 410 recording_source->SetRequiresClear(true); | |
| 411 recording_source->SetClearCanvasWithDebugColor(false); | |
| 412 | |
| 413 // First record everything as white. | |
| 414 const unsigned alpha_dark = 10u; | |
| 415 PaintFlags white_flags; | |
| 416 white_flags.setColor(SK_ColorWHITE); | |
| 417 white_flags.setAlpha(alpha_dark); | |
| 418 recording_source->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 419 white_flags); | |
| 420 recording_source->Rerecord(); | |
| 421 | |
| 422 scoped_refptr<RasterSource> raster = | |
| 423 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 424 | |
| 425 gfx::Size content_bounds( | |
| 426 gfx::ScaleToCeiledSize(layer_bounds, contents_scale)); | |
| 427 | |
| 428 SkBitmap bitmap; | |
| 429 bitmap.allocN32Pixels(content_bounds.width(), content_bounds.height()); | |
| 430 SkCanvas canvas(bitmap); | |
| 431 canvas.clear(SK_ColorTRANSPARENT); | |
| 432 | |
| 433 // Playback the full rect which should make everything light gray (alpha=10). | |
| 434 gfx::Rect raster_full_rect(content_bounds); | |
| 435 gfx::Rect playback_rect(content_bounds); | |
| 436 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect, | |
| 437 contents_scale, RasterSource::PlaybackSettings()); | |
| 438 | |
| 439 { | |
| 440 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 441 for (int i = 0; i < bitmap.width(); ++i) { | |
| 442 for (int j = 0; j < bitmap.height(); ++j) { | |
| 443 SCOPED_TRACE(i); | |
| 444 SCOPED_TRACE(j); | |
| 445 EXPECT_EQ(alpha_dark, SkColorGetA(pixels[i + j * bitmap.width()])); | |
| 446 EXPECT_EQ(alpha_dark, SkColorGetR(pixels[i + j * bitmap.width()])); | |
| 447 EXPECT_EQ(alpha_dark, SkColorGetG(pixels[i + j * bitmap.width()])); | |
| 448 EXPECT_EQ(alpha_dark, SkColorGetB(pixels[i + j * bitmap.width()])); | |
| 449 } | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 std::unique_ptr<FakeRecordingSource> recording_source_light = | |
| 454 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 455 recording_source_light->SetBackgroundColor(SK_ColorGREEN); | |
| 456 recording_source_light->SetRequiresClear(true); | |
| 457 recording_source_light->SetClearCanvasWithDebugColor(false); | |
| 458 | |
| 459 // Record everything as a slightly lighter white. | |
| 460 const unsigned alpha_light = 18u; | |
| 461 white_flags.setAlpha(alpha_light); | |
| 462 recording_source_light->add_draw_rect_with_flags(gfx::Rect(layer_bounds), | |
| 463 white_flags); | |
| 464 recording_source_light->Rerecord(); | |
| 465 | |
| 466 // Make a new RasterSource from the new recording. | |
| 467 raster = RasterSource::CreateFromRecordingSource(recording_source_light.get(), | |
| 468 false); | |
| 469 | |
| 470 // We're going to playback from alpha(18) white rectangle into a smaller area | |
| 471 // of the recording resulting in a smaller lighter white rectangle over a | |
| 472 // darker white background rectangle. | |
| 473 playback_rect = | |
| 474 gfx::Rect(gfx::ScaleToCeiledSize(partial_bounds, contents_scale)); | |
| 475 raster->PlaybackToCanvas(&canvas, raster_full_rect, playback_rect, | |
| 476 contents_scale, RasterSource::PlaybackSettings()); | |
| 477 | |
| 478 // Test that the whole playback_rect was cleared and repainted with new alpha. | |
| 479 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 480 for (int i = 0; i < playback_rect.width(); ++i) { | |
| 481 for (int j = 0; j < playback_rect.height(); ++j) { | |
| 482 SCOPED_TRACE(j); | |
| 483 SCOPED_TRACE(i); | |
| 484 EXPECT_EQ(alpha_light, SkColorGetA(pixels[i + j * bitmap.width()])); | |
| 485 EXPECT_EQ(alpha_light, SkColorGetR(pixels[i + j * bitmap.width()])); | |
| 486 EXPECT_EQ(alpha_light, SkColorGetG(pixels[i + j * bitmap.width()])); | |
| 487 EXPECT_EQ(alpha_light, SkColorGetB(pixels[i + j * bitmap.width()])); | |
| 488 } | |
| 489 } | |
| 490 } | |
| 491 | |
| 492 TEST(RasterSourceTest, RasterContentsTransparent) { | |
| 493 gfx::Size layer_bounds(5, 3); | |
| 494 float contents_scale = 0.5f; | |
| 495 | |
| 496 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 497 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 498 recording_source->SetBackgroundColor(SK_ColorTRANSPARENT); | |
| 499 recording_source->SetRequiresClear(true); | |
| 500 recording_source->SetClearCanvasWithDebugColor(false); | |
| 501 recording_source->Rerecord(); | |
| 502 | |
| 503 scoped_refptr<RasterSource> raster = | |
| 504 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 505 gfx::Size content_bounds( | |
| 506 gfx::ScaleToCeiledSize(layer_bounds, contents_scale)); | |
| 507 | |
| 508 gfx::Rect canvas_rect(content_bounds); | |
| 509 canvas_rect.Inset(0, 0, -1, -1); | |
| 510 | |
| 511 SkBitmap bitmap; | |
| 512 bitmap.allocN32Pixels(canvas_rect.width(), canvas_rect.height()); | |
| 513 SkCanvas canvas(bitmap); | |
| 514 | |
| 515 raster->PlaybackToCanvas(&canvas, canvas_rect, canvas_rect, contents_scale, | |
| 516 RasterSource::PlaybackSettings()); | |
| 517 | |
| 518 SkColor* pixels = reinterpret_cast<SkColor*>(bitmap.getPixels()); | |
| 519 int num_pixels = bitmap.width() * bitmap.height(); | |
| 520 for (int i = 0; i < num_pixels; ++i) { | |
| 521 EXPECT_EQ(SkColorGetA(pixels[i]), 0u); | |
| 522 } | |
| 523 } | |
| 524 | |
| 525 TEST(RasterSourceTest, GetPictureMemoryUsageIncludesClientReportedMemory) { | |
| 526 const size_t kReportedMemoryUsageInBytes = 100 * 1024 * 1024; | |
| 527 gfx::Size layer_bounds(5, 3); | |
| 528 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 529 FakeRecordingSource::CreateFilledRecordingSource(layer_bounds); | |
| 530 recording_source->set_reported_memory_usage(kReportedMemoryUsageInBytes); | |
| 531 recording_source->Rerecord(); | |
| 532 | |
| 533 scoped_refptr<RasterSource> raster = | |
| 534 RasterSource::CreateFromRecordingSource(recording_source.get(), false); | |
| 535 size_t total_memory_usage = raster->GetMemoryUsage(); | |
| 536 EXPECT_GE(total_memory_usage, kReportedMemoryUsageInBytes); | |
| 537 EXPECT_LT(total_memory_usage, 2 * kReportedMemoryUsageInBytes); | |
| 538 } | |
| 539 | |
| 540 TEST(RasterSourceTest, ImageHijackCanvasRespectsSharedCanvasTransform) { | |
| 541 gfx::Size size(100, 100); | |
| 542 | |
| 543 // Create a recording source that is filled with red and every corner is | |
| 544 // green (4x4 rects in the corner are green to account for blending when | |
| 545 // scaling). Note that we paint an image first, so that we can force image | |
| 546 // hijack canvas to be used. | |
| 547 std::unique_ptr<FakeRecordingSource> recording_source = | |
| 548 FakeRecordingSource::CreateFilledRecordingSource(size); | |
| 549 | |
| 550 // 1. Paint the image. | |
| 551 recording_source->add_draw_image(CreateDiscardableImage(gfx::Size(5, 5)), | |
| 552 gfx::Point(0, 0)); | |
| 553 | |
| 554 // 2. Cover everything in red. | |
| 555 PaintFlags flags; | |
| 556 flags.setColor(SK_ColorRED); | |
| 557 recording_source->add_draw_rect_with_flags(gfx::Rect(size), flags); | |
| 558 | |
| 559 // 3. Draw 4x4 green rects into every corner. | |
| 560 flags.setColor(SK_ColorGREEN); | |
| 561 recording_source->add_draw_rect_with_flags(gfx::Rect(0, 0, 4, 4), flags); | |
| 562 recording_source->add_draw_rect_with_flags( | |
| 563 gfx::Rect(size.width() - 4, 0, 4, 4), flags); | |
| 564 recording_source->add_draw_rect_with_flags( | |
| 565 gfx::Rect(0, size.height() - 4, 4, 4), flags); | |
| 566 recording_source->add_draw_rect_with_flags( | |
| 567 gfx::Rect(size.width() - 4, size.height() - 4, 4, 4), flags); | |
| 568 | |
| 569 recording_source->SetGenerateDiscardableImagesMetadata(true); | |
| 570 recording_source->Rerecord(); | |
| 571 | |
| 572 bool can_use_lcd = true; | |
| 573 scoped_refptr<RasterSource> raster_source = | |
| 574 recording_source->CreateRasterSource(can_use_lcd); | |
| 575 SoftwareImageDecodeCache controller( | |
| 576 ResourceFormat::RGBA_8888, | |
| 577 LayerTreeSettings().software_decoded_image_budget_bytes); | |
| 578 raster_source->set_image_decode_cache(&controller); | |
| 579 | |
| 580 SkBitmap bitmap; | |
| 581 bitmap.allocN32Pixels(size.width() * 0.5f, size.height() * 0.25f); | |
| 582 SkCanvas canvas(bitmap); | |
| 583 canvas.scale(0.5f, 0.25f); | |
| 584 | |
| 585 RasterSource::PlaybackSettings settings; | |
| 586 settings.playback_to_shared_canvas = true; | |
| 587 settings.use_image_hijack_canvas = true; | |
| 588 raster_source->PlaybackToCanvas(&canvas, gfx::Rect(size), gfx::Rect(size), | |
| 589 1.f, settings); | |
| 590 | |
| 591 EXPECT_EQ(SK_ColorGREEN, bitmap.getColor(0, 0)); | |
| 592 EXPECT_EQ(SK_ColorGREEN, bitmap.getColor(49, 0)); | |
| 593 EXPECT_EQ(SK_ColorGREEN, bitmap.getColor(0, 24)); | |
| 594 EXPECT_EQ(SK_ColorGREEN, bitmap.getColor(49, 24)); | |
| 595 for (int x = 0; x < 49; ++x) | |
| 596 EXPECT_EQ(SK_ColorRED, bitmap.getColor(x, 12)); | |
| 597 for (int y = 0; y < 24; ++y) | |
| 598 EXPECT_EQ(SK_ColorRED, bitmap.getColor(24, y)); | |
| 599 } | |
| 600 | |
| 601 } // namespace | |
| 602 } // namespace cc | |
| OLD | NEW |