| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 m_image = surface->makeImageSnapshot(); | 59 m_image = surface->makeImageSnapshot(); |
| 60 | 60 |
| 61 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); | 61 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); |
| 62 surface2->getCanvas()->clear(0xAAAAAAAA); | 62 surface2->getCanvas()->clear(0xAAAAAAAA); |
| 63 m_image2 = surface2->makeImageSnapshot(); | 63 m_image2 = surface2->makeImageSnapshot(); |
| 64 | 64 |
| 65 // Save the global memory cache to restore it upon teardown. | 65 // Save the global memory cache to restore it upon teardown. |
| 66 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); | 66 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); |
| 67 | 67 |
| 68 // Save the state of experimental canvas features and color correct | 68 // Save the state of experimental canvas features and color correct |
| 69 // rendering flags to restore them on teardown. Each test that changes the | 69 // rendering flags to restore them on teardown. |
| 70 // flags must restore them to prevent affecting other ImageBitmap tests. | |
| 71 // This is an extra safety precaution to prevent such an error to leak from | |
| 72 // this test suite. | |
| 73 experimentalCanvasFeatures = | 70 experimentalCanvasFeatures = |
| 74 RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); | 71 RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); |
| 75 colorCorrectRendering = | 72 colorCorrectRendering = |
| 76 RuntimeEnabledFeatures::colorCorrectRenderingEnabled(); | 73 RuntimeEnabledFeatures::colorCorrectRenderingEnabled(); |
| 77 colorCorrectRenderingDefaultMode = | 74 colorCorrectRenderingDefaultMode = |
| 78 RuntimeEnabledFeatures::colorCorrectRenderingDefaultModeEnabled(); | 75 RuntimeEnabledFeatures::colorCorrectRenderingDefaultModeEnabled(); |
| 79 } | 76 } |
| 80 virtual void TearDown() { | 77 virtual void TearDown() { |
| 81 // Garbage collection is required prior to switching out the | 78 // Garbage collection is required prior to switching out the |
| 82 // test's memory cache; image resources are released, evicting | 79 // test's memory cache; image resources are released, evicting |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 new uint8_t[imageInfo.bytesPerPixel()]()); | 436 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 440 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, | 437 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, |
| 441 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); | 438 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); |
| 442 | 439 |
| 443 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), | 440 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), |
| 444 imageInfo.bytesPerPixel()); | 441 imageInfo.bytesPerPixel()); |
| 445 ASSERT_EQ(compare, 0); | 442 ASSERT_EQ(compare, 0); |
| 446 } | 443 } |
| 447 } | 444 } |
| 448 | 445 |
| 446 TEST_F(ImageBitmapTest, ImageBitmapColorSpaceConversionStaticBitmapImage) { |
| 447 SkPaint p; |
| 448 p.setColor(SK_ColorRED); |
| 449 sk_sp<SkColorSpace> srcRGBColorSpace = |
| 450 SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 451 |
| 452 SkImageInfo rasterImageInfo = |
| 453 SkImageInfo::MakeN32Premul(10, 10, srcRGBColorSpace); |
| 454 sk_sp<SkSurface> surface(SkSurface::MakeRaster(rasterImageInfo)); |
| 455 surface->getCanvas()->drawCircle(5, 5, 5, p); |
| 456 sk_sp<SkImage> image = surface->makeImageSnapshot(); |
| 457 |
| 458 std::unique_ptr<uint8_t[]> srcPixel( |
| 459 new uint8_t[rasterImageInfo.bytesPerPixel()]()); |
| 460 image->readPixels(rasterImageInfo.makeWH(1, 1), srcPixel.get(), |
| 461 image->width() * rasterImageInfo.bytesPerPixel(), 5, 5); |
| 462 |
| 463 Optional<IntRect> cropRect = IntRect(0, 0, image->width(), image->height()); |
| 464 |
| 465 sk_sp<SkColorSpace> colorSpace = nullptr; |
| 466 SkColorType colorType = SkColorType::kN32_SkColorType; |
| 467 SkColorSpaceXform::ColorFormat colorFormat32 = |
| 468 (colorType == kBGRA_8888_SkColorType) |
| 469 ? SkColorSpaceXform::ColorFormat::kBGRA_8888_ColorFormat |
| 470 : SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat; |
| 471 SkColorSpaceXform::ColorFormat colorFormat = colorFormat32; |
| 472 |
| 473 for (uint8_t i = static_cast<uint8_t>( |
| 474 ColorSpaceConversion::DEFAULT_NOT_COLOR_CORRECTED); |
| 475 i <= static_cast<uint8_t>(ColorSpaceConversion::LAST); i++) { |
| 476 ColorSpaceConversion colorSpaceConversion = |
| 477 static_cast<ColorSpaceConversion>(i); |
| 478 ImageBitmapOptions options = |
| 479 prepareBitmapOptionsAndSetRuntimeFlags(colorSpaceConversion); |
| 480 ImageBitmap* imageBitmap = ImageBitmap::create( |
| 481 StaticBitmapImage::create(image), cropRect, options); |
| 482 |
| 483 // ColorBehavior::ignore() is used instead of |
| 484 // ColorBehavior::transformToTargetForTesting() to avoid color conversion to |
| 485 // display color profile, as we want to solely rely on the color correction |
| 486 // that happens in ImageBitmap create method. |
| 487 SkImage* convertedImage = |
| 488 imageBitmap->bitmapImage() |
| 489 ->imageForCurrentFrame(ColorBehavior::ignore()) |
| 490 .get(); |
| 491 |
| 492 switch (colorSpaceConversion) { |
| 493 case ColorSpaceConversion::NONE: |
| 494 NOTREACHED(); |
| 495 break; |
| 496 case ColorSpaceConversion::DEFAULT_NOT_COLOR_CORRECTED: |
| 497 // TODO(zakerinasab): Replace sRGB with a call to |
| 498 // ImageDecoder::globalTargetColorSpace() when the crash problem on Mac |
| 499 // is fixed. crbug.com/668546. |
| 500 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 501 colorFormat = colorFormat32; |
| 502 break; |
| 503 case ColorSpaceConversion::DEFAULT_COLOR_CORRECTED: |
| 504 case ColorSpaceConversion::SRGB: |
| 505 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 506 colorFormat = colorFormat32; |
| 507 break; |
| 508 case ColorSpaceConversion::LINEAR_RGB: |
| 509 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 510 colorType = SkColorType::kRGBA_F16_SkColorType; |
| 511 colorFormat = SkColorSpaceXform::ColorFormat::kRGBA_F16_ColorFormat; |
| 512 break; |
| 513 default: |
| 514 NOTREACHED(); |
| 515 } |
| 516 |
| 517 SkImageInfo imageInfo = SkImageInfo::Make( |
| 518 1, 1, colorType, SkAlphaType::kPremul_SkAlphaType, colorSpace); |
| 519 std::unique_ptr<uint8_t[]> convertedPixel( |
| 520 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 521 convertedImage->readPixels( |
| 522 imageInfo, convertedPixel.get(), |
| 523 convertedImage->width() * imageInfo.bytesPerPixel(), 5, 5); |
| 524 |
| 525 // Transform the source pixel and check if the image bitmap color conversion |
| 526 // is done correctly. |
| 527 std::unique_ptr<SkColorSpaceXform> colorSpaceXform = |
| 528 SkColorSpaceXform::New(srcRGBColorSpace.get(), colorSpace.get()); |
| 529 std::unique_ptr<uint8_t[]> transformedPixel( |
| 530 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 531 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, |
| 532 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); |
| 533 |
| 534 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), |
| 535 imageInfo.bytesPerPixel()); |
| 536 ASSERT_EQ(compare, 0); |
| 537 } |
| 538 } |
| 539 |
| 449 } // namespace blink | 540 } // namespace blink |
| OLD | NEW |