| 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 22 matching lines...) Expand all Loading... |
| 33 #include "SkPixelRef.h" // FIXME: qualify this skia header file. | 33 #include "SkPixelRef.h" // FIXME: qualify this skia header file. |
| 34 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
| 35 #include "core/fetch/ImageResource.h" | 35 #include "core/fetch/ImageResource.h" |
| 36 #include "core/fetch/MemoryCache.h" | 36 #include "core/fetch/MemoryCache.h" |
| 37 #include "core/html/HTMLCanvasElement.h" | 37 #include "core/html/HTMLCanvasElement.h" |
| 38 #include "core/html/HTMLImageElement.h" | 38 #include "core/html/HTMLImageElement.h" |
| 39 #include "core/html/HTMLVideoElement.h" | 39 #include "core/html/HTMLVideoElement.h" |
| 40 #include "platform/graphics/StaticBitmapImage.h" | 40 #include "platform/graphics/StaticBitmapImage.h" |
| 41 #include "platform/graphics/skia/SkiaUtils.h" | 41 #include "platform/graphics/skia/SkiaUtils.h" |
| 42 #include "platform/heap/Handle.h" | 42 #include "platform/heap/Handle.h" |
| 43 #include "platform/image-decoders/ImageDecoder.h" |
| 43 #include "platform/network/ResourceRequest.h" | 44 #include "platform/network/ResourceRequest.h" |
| 44 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
| 45 #include "third_party/skia/include/core/SkCanvas.h" | 46 #include "third_party/skia/include/core/SkCanvas.h" |
| 47 #include "third_party/skia/include/core/SkColorSpaceXform.h" |
| 46 #include "third_party/skia/include/core/SkImage.h" | 48 #include "third_party/skia/include/core/SkImage.h" |
| 47 #include "third_party/skia/include/core/SkSurface.h" | 49 #include "third_party/skia/include/core/SkSurface.h" |
| 48 | 50 |
| 49 namespace blink { | 51 namespace blink { |
| 50 | 52 |
| 51 class ImageBitmapTest : public ::testing::Test { | 53 class ImageBitmapTest : public ::testing::Test { |
| 52 protected: | 54 protected: |
| 53 virtual void SetUp() { | 55 virtual void SetUp() { |
| 54 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(10, 10); | 56 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(10, 10); |
| 55 surface->getCanvas()->clear(0xFFFFFFFF); | 57 surface->getCanvas()->clear(0xFFFFFFFF); |
| 56 m_image = surface->makeImageSnapshot(); | 58 m_image = surface->makeImageSnapshot(); |
| 57 | 59 |
| 58 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); | 60 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); |
| 59 surface2->getCanvas()->clear(0xAAAAAAAA); | 61 surface2->getCanvas()->clear(0xAAAAAAAA); |
| 60 m_image2 = surface2->makeImageSnapshot(); | 62 m_image2 = surface2->makeImageSnapshot(); |
| 61 | 63 |
| 62 // Save the global memory cache to restore it upon teardown. | 64 // Save the global memory cache to restore it upon teardown. |
| 63 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); | 65 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()); |
| 66 |
| 67 // Save the state of experimental canvas features and color correct |
| 68 // rendering flags to restore them on teardown. |
| 69 experimentalCanvasFeatures = |
| 70 RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); |
| 71 colorCorrectRendering = |
| 72 RuntimeEnabledFeatures::colorCorrectRenderingEnabled(); |
| 73 colorCorrectRenderingDefaultMode = |
| 74 RuntimeEnabledFeatures::colorCorrectRenderingDefaultModeEnabled(); |
| 64 } | 75 } |
| 65 virtual void TearDown() { | 76 virtual void TearDown() { |
| 66 // Garbage collection is required prior to switching out the | 77 // Garbage collection is required prior to switching out the |
| 67 // test's memory cache; image resources are released, evicting | 78 // test's memory cache; image resources are released, evicting |
| 68 // them from the cache. | 79 // them from the cache. |
| 69 ThreadState::current()->collectGarbage(BlinkGC::NoHeapPointersOnStack, | 80 ThreadState::current()->collectGarbage(BlinkGC::NoHeapPointersOnStack, |
| 70 BlinkGC::GCWithSweep, | 81 BlinkGC::GCWithSweep, |
| 71 BlinkGC::ForcedGC); | 82 BlinkGC::ForcedGC); |
| 72 | 83 |
| 73 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | 84 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); |
| 85 RuntimeEnabledFeatures::setExperimentalCanvasFeaturesEnabled( |
| 86 experimentalCanvasFeatures); |
| 87 RuntimeEnabledFeatures::setColorCorrectRenderingEnabled( |
| 88 colorCorrectRendering); |
| 89 RuntimeEnabledFeatures::setColorCorrectRenderingDefaultModeEnabled( |
| 90 colorCorrectRenderingDefaultMode); |
| 74 } | 91 } |
| 75 | 92 |
| 76 sk_sp<SkImage> m_image, m_image2; | 93 sk_sp<SkImage> m_image, m_image2; |
| 77 Persistent<MemoryCache> m_globalMemoryCache; | 94 Persistent<MemoryCache> m_globalMemoryCache; |
| 95 bool experimentalCanvasFeatures; |
| 96 bool colorCorrectRendering; |
| 97 bool colorCorrectRenderingDefaultMode; |
| 78 }; | 98 }; |
| 79 | 99 |
| 80 TEST_F(ImageBitmapTest, ImageResourceConsistency) { | 100 TEST_F(ImageBitmapTest, ImageResourceConsistency) { |
| 81 const ImageBitmapOptions defaultOptions; | 101 const ImageBitmapOptions defaultOptions; |
| 82 HTMLImageElement* imageElement = | 102 HTMLImageElement* imageElement = |
| 83 HTMLImageElement::create(*Document::create()); | 103 HTMLImageElement::create(*Document::create()); |
| 84 ImageResource* image = | 104 ImageResource* image = |
| 85 ImageResource::create(StaticBitmapImage::create(m_image).get()); | 105 ImageResource::create(StaticBitmapImage::create(m_image).get()); |
| 86 imageElement->setImageResource(image); | 106 imageElement->setImageResource(image); |
| 87 | 107 |
| 88 Optional<IntRect> cropRect = | 108 Optional<IntRect> cropRect = |
| 89 IntRect(0, 0, m_image->width(), m_image->height()); | 109 IntRect(0, 0, m_image->width(), m_image->height()); |
| 90 ImageBitmap* imageBitmapNoCrop = ImageBitmap::create( | 110 ImageBitmap* imageBitmapNoCrop = ImageBitmap::create( |
| 91 imageElement, cropRect, &(imageElement->document()), defaultOptions); | 111 imageElement, cropRect, &(imageElement->document()), defaultOptions); |
| 92 cropRect = IntRect(m_image->width() / 2, m_image->height() / 2, | 112 cropRect = IntRect(m_image->width() / 2, m_image->height() / 2, |
| 93 m_image->width() / 2, m_image->height() / 2); | 113 m_image->width() / 2, m_image->height() / 2); |
| 94 ImageBitmap* imageBitmapInteriorCrop = ImageBitmap::create( | 114 ImageBitmap* imageBitmapInteriorCrop = ImageBitmap::create( |
| 95 imageElement, cropRect, &(imageElement->document()), defaultOptions); | 115 imageElement, cropRect, &(imageElement->document()), defaultOptions); |
| 96 cropRect = IntRect(-m_image->width() / 2, -m_image->height() / 2, | 116 cropRect = IntRect(-m_image->width() / 2, -m_image->height() / 2, |
| 97 m_image->width(), m_image->height()); | 117 m_image->width(), m_image->height()); |
| 98 ImageBitmap* imageBitmapExteriorCrop = ImageBitmap::create( | 118 ImageBitmap* imageBitmapExteriorCrop = ImageBitmap::create( |
| 99 imageElement, cropRect, &(imageElement->document()), defaultOptions); | 119 imageElement, cropRect, &(imageElement->document()), defaultOptions); |
| 100 cropRect = IntRect(-m_image->width(), -m_image->height(), m_image->width(), | 120 cropRect = IntRect(-m_image->width(), -m_image->height(), m_image->width(), |
| 101 m_image->height()); | 121 m_image->height()); |
| 102 ImageBitmap* imageBitmapOutsideCrop = ImageBitmap::create( | 122 ImageBitmap* imageBitmapOutsideCrop = ImageBitmap::create( |
| 103 imageElement, cropRect, &(imageElement->document()), defaultOptions); | 123 imageElement, cropRect, &(imageElement->document()), defaultOptions); |
| 104 | 124 |
| 105 ASSERT_EQ(imageBitmapNoCrop->bitmapImage()->imageForCurrentFrame(), | 125 ASSERT_NE(imageBitmapNoCrop->bitmapImage()->imageForCurrentFrame(), |
| 106 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); | 126 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); |
| 107 ASSERT_NE(imageBitmapInteriorCrop->bitmapImage()->imageForCurrentFrame(), | 127 ASSERT_NE(imageBitmapInteriorCrop->bitmapImage()->imageForCurrentFrame(), |
| 108 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); | 128 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); |
| 109 ASSERT_NE(imageBitmapExteriorCrop->bitmapImage()->imageForCurrentFrame(), | 129 ASSERT_NE(imageBitmapExteriorCrop->bitmapImage()->imageForCurrentFrame(), |
| 110 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); | 130 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); |
| 111 | 131 |
| 112 StaticBitmapImage* emptyImage = imageBitmapOutsideCrop->bitmapImage(); | 132 StaticBitmapImage* emptyImage = imageBitmapOutsideCrop->bitmapImage(); |
| 113 ASSERT_NE(emptyImage->imageForCurrentFrame(), | 133 ASSERT_NE(emptyImage->imageForCurrentFrame(), |
| 114 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); | 134 imageElement->cachedImage()->getImage()->imageForCurrentFrame()); |
| 115 } | 135 } |
| 116 | 136 |
| 117 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a | 137 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a |
| 118 // reference to the original Image if the HTMLImageElement src is changed. | 138 // reference to the original Image if the HTMLImageElement src is changed. |
| 119 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) { | 139 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) { |
| 120 HTMLImageElement* image = HTMLImageElement::create(*Document::create()); | 140 HTMLImageElement* image = HTMLImageElement::create(*Document::create()); |
| 121 ImageResource* originalImageResource = | 141 ImageResource* originalImageResource = |
| 122 ImageResource::create(StaticBitmapImage::create(m_image).get()); | 142 ImageResource::create(StaticBitmapImage::create(m_image).get()); |
| 123 image->setImageResource(originalImageResource); | 143 image->setImageResource(originalImageResource); |
| 124 | 144 |
| 125 const ImageBitmapOptions defaultOptions; | 145 const ImageBitmapOptions defaultOptions; |
| 126 Optional<IntRect> cropRect = | 146 Optional<IntRect> cropRect = |
| 127 IntRect(0, 0, m_image->width(), m_image->height()); | 147 IntRect(0, 0, m_image->width(), m_image->height()); |
| 128 ImageBitmap* imageBitmap = ImageBitmap::create( | 148 ImageBitmap* imageBitmap = ImageBitmap::create( |
| 129 image, cropRect, &(image->document()), defaultOptions); | 149 image, cropRect, &(image->document()), defaultOptions); |
| 130 ASSERT_EQ(imageBitmap->bitmapImage()->imageForCurrentFrame(), | 150 // As we are applying color space conversion for the "default" mode, |
| 151 // this verifies that the color corrected image is not the same as the |
| 152 // source. |
| 153 ASSERT_NE(imageBitmap->bitmapImage()->imageForCurrentFrame(), |
| 131 originalImageResource->getImage()->imageForCurrentFrame()); | 154 originalImageResource->getImage()->imageForCurrentFrame()); |
| 132 | 155 |
| 133 ImageResource* newImageResource = | 156 ImageResource* newImageResource = |
| 134 ImageResource::create(StaticBitmapImage::create(m_image2).get()); | 157 ImageResource::create(StaticBitmapImage::create(m_image2).get()); |
| 135 image->setImageResource(newImageResource); | 158 image->setImageResource(newImageResource); |
| 136 | 159 |
| 137 // The ImageBitmap should contain the same data as the original cached image | |
| 138 { | 160 { |
| 139 ASSERT_EQ(imageBitmap->bitmapImage()->imageForCurrentFrame(), | 161 ASSERT_NE(imageBitmap->bitmapImage()->imageForCurrentFrame(), |
| 140 originalImageResource->getImage()->imageForCurrentFrame()); | 162 originalImageResource->getImage()->imageForCurrentFrame()); |
| 141 SkImage* image1 = imageBitmap->bitmapImage()->imageForCurrentFrame().get(); | 163 SkImage* image1 = imageBitmap->bitmapImage()->imageForCurrentFrame().get(); |
| 142 ASSERT_NE(image1, nullptr); | 164 ASSERT_NE(image1, nullptr); |
| 143 SkImage* image2 = | 165 SkImage* image2 = |
| 144 originalImageResource->getImage()->imageForCurrentFrame().get(); | 166 originalImageResource->getImage()->imageForCurrentFrame().get(); |
| 145 ASSERT_NE(image2, nullptr); | 167 ASSERT_NE(image2, nullptr); |
| 146 ASSERT_EQ(image1, image2); | 168 ASSERT_NE(image1, image2); |
| 147 } | 169 } |
| 148 | 170 |
| 149 { | 171 { |
| 150 ASSERT_NE(imageBitmap->bitmapImage()->imageForCurrentFrame(), | 172 ASSERT_NE(imageBitmap->bitmapImage()->imageForCurrentFrame(), |
| 151 newImageResource->getImage()->imageForCurrentFrame()); | 173 newImageResource->getImage()->imageForCurrentFrame()); |
| 152 SkImage* image1 = imageBitmap->bitmapImage()->imageForCurrentFrame().get(); | 174 SkImage* image1 = imageBitmap->bitmapImage()->imageForCurrentFrame().get(); |
| 153 ASSERT_NE(image1, nullptr); | 175 ASSERT_NE(image1, nullptr); |
| 154 SkImage* image2 = | 176 SkImage* image2 = |
| 155 newImageResource->getImage()->imageForCurrentFrame().get(); | 177 newImageResource->getImage()->imageForCurrentFrame().get(); |
| 156 ASSERT_NE(image2, nullptr); | 178 ASSERT_NE(image2, nullptr); |
| 157 ASSERT_NE(image1, image2); | 179 ASSERT_NE(image1, image2); |
| 158 } | 180 } |
| 159 } | 181 } |
| 160 | 182 |
| 183 enum class ColorSpaceConversion : uint8_t { |
| 184 NONE = 0, |
| 185 DEFAULT_NOT_COLOR_CORRECTED = 1, |
| 186 DEFAULT_COLOR_CORRECTED = 2, |
| 187 SRGB = 3, |
| 188 LINEAR_RGB = 4, |
| 189 |
| 190 LAST = LINEAR_RGB |
| 191 }; |
| 192 |
| 193 static ImageBitmap* createImageBitmapWithColorSpaceConversion( |
| 194 HTMLImageElement* image, |
| 195 Optional<IntRect>& cropRect, |
| 196 Document* document, |
| 197 const ColorSpaceConversion& colorSpaceConversion) { |
| 198 // Set the color space conversion in ImageBitmapOptions |
| 199 ImageBitmapOptions options; |
| 200 static const Vector<String> conversions = {"none", "default", "default", |
| 201 "srgb", "linear-rgb"}; |
| 202 options.setColorSpaceConversion( |
| 203 conversions[static_cast<uint8_t>(colorSpaceConversion)]); |
| 204 |
| 205 // Set the runtime flags |
| 206 bool flag = (colorSpaceConversion != |
| 207 ColorSpaceConversion::DEFAULT_NOT_COLOR_CORRECTED); |
| 208 RuntimeEnabledFeatures::setExperimentalCanvasFeaturesEnabled(true); |
| 209 RuntimeEnabledFeatures::setColorCorrectRenderingEnabled(flag); |
| 210 RuntimeEnabledFeatures::setColorCorrectRenderingDefaultModeEnabled(!flag); |
| 211 |
| 212 // Create and return the ImageBitmap |
| 213 return ImageBitmap::create(image, cropRect, &(image->document()), options); |
| 214 } |
| 215 |
| 216 TEST_F(ImageBitmapTest, ImageBitmapColorSpaceConversion) { |
| 217 HTMLImageElement* imageElement = |
| 218 HTMLImageElement::create(*Document::create()); |
| 219 |
| 220 SkPaint p; |
| 221 p.setColor(SK_ColorRED); |
| 222 sk_sp<SkColorSpace> srcRGBColorSpace = |
| 223 SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 224 |
| 225 SkImageInfo rasterImageInfo = |
| 226 SkImageInfo::MakeN32Premul(100, 100, srcRGBColorSpace); |
| 227 sk_sp<SkSurface> surface(SkSurface::MakeRaster(rasterImageInfo)); |
| 228 surface->getCanvas()->drawCircle(50, 50, 50, p); |
| 229 sk_sp<SkImage> image = surface->makeImageSnapshot(); |
| 230 |
| 231 auto srcPixel = WTF::makeUnique<uint8_t>(rasterImageInfo.bytesPerPixel()); |
| 232 image->readPixels(rasterImageInfo.makeWH(1, 1), srcPixel.get(), |
| 233 image->width() * rasterImageInfo.bytesPerPixel(), 50, 50); |
| 234 |
| 235 ImageResource* originalImageResource = |
| 236 ImageResource::create(StaticBitmapImage::create(image).get()); |
| 237 imageElement->setImageResource(originalImageResource); |
| 238 |
| 239 Optional<IntRect> cropRect = IntRect(0, 0, image->width(), image->height()); |
| 240 |
| 241 // Create and test the ImageBitmap objects. |
| 242 // We don't check "none" color space conversion as it requires the encoded |
| 243 // data in a format readable by ImageDecoder. Furthermore, the code path for |
| 244 // "none" color space conversion is not affected by this CL. |
| 245 |
| 246 sk_sp<SkColorSpace> colorSpace = nullptr; |
| 247 SkColorType colorType = SkColorType::kN32_SkColorType; |
| 248 SkColorSpaceXform::ColorFormat colorFormat32 = |
| 249 (colorType == kBGRA_8888_SkColorType) |
| 250 ? SkColorSpaceXform::ColorFormat::kBGRA_8888_ColorFormat |
| 251 : SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat; |
| 252 SkColorSpaceXform::ColorFormat colorFormat = colorFormat32; |
| 253 |
| 254 for (uint8_t i = static_cast<uint8_t>( |
| 255 ColorSpaceConversion::DEFAULT_NOT_COLOR_CORRECTED); |
| 256 i <= static_cast<uint8_t>(ColorSpaceConversion::LAST); i++) { |
| 257 ColorSpaceConversion colorSpaceConversion = |
| 258 static_cast<ColorSpaceConversion>(i); |
| 259 ImageBitmap* imageBitmap = createImageBitmapWithColorSpaceConversion( |
| 260 imageElement, cropRect, &(imageElement->document()), |
| 261 colorSpaceConversion); |
| 262 SkImage* convertedImage = |
| 263 imageBitmap->bitmapImage()->imageForCurrentFrame().get(); |
| 264 |
| 265 switch (colorSpaceConversion) { |
| 266 case ColorSpaceConversion::NONE: |
| 267 NOTREACHED(); |
| 268 break; |
| 269 case ColorSpaceConversion::DEFAULT_NOT_COLOR_CORRECTED: |
| 270 // TODO(zakerinasab): Replace sRGB with a call to |
| 271 // ImageDecoder::globalTargetColorSpace() when the crash problem on Mac |
| 272 // is fixed. crbug.com/668546. |
| 273 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 274 colorFormat = colorFormat32; |
| 275 break; |
| 276 case ColorSpaceConversion::DEFAULT_COLOR_CORRECTED: |
| 277 case ColorSpaceConversion::SRGB: |
| 278 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 279 colorFormat = colorFormat32; |
| 280 break; |
| 281 case ColorSpaceConversion::LINEAR_RGB: |
| 282 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 283 colorType = SkColorType::kRGBA_F16_SkColorType; |
| 284 colorFormat = SkColorSpaceXform::ColorFormat::kRGBA_F16_ColorFormat; |
| 285 break; |
| 286 default: |
| 287 NOTREACHED(); |
| 288 } |
| 289 |
| 290 SkImageInfo imageInfo = SkImageInfo::Make( |
| 291 1, 1, colorType, SkAlphaType::kPremul_SkAlphaType, colorSpace); |
| 292 auto convertedPixel = WTF::makeUnique<uint8_t>(imageInfo.bytesPerPixel()); |
| 293 convertedImage->readPixels( |
| 294 imageInfo, convertedPixel.get(), |
| 295 convertedImage->width() * imageInfo.bytesPerPixel(), 50, 50); |
| 296 |
| 297 // Transform the source pixel and check if the image bitmap color conversion |
| 298 // is done correctly. |
| 299 std::unique_ptr<SkColorSpaceXform> colorSpaceXform = |
| 300 SkColorSpaceXform::New(srcRGBColorSpace.get(), colorSpace.get()); |
| 301 auto transformedPixel = WTF::makeUnique<uint8_t>(imageInfo.bytesPerPixel()); |
| 302 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, |
| 303 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); |
| 304 |
| 305 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), |
| 306 imageInfo.bytesPerPixel()); |
| 307 ASSERT_EQ(compare, 0); |
| 308 } |
| 309 } |
| 310 |
| 161 } // namespace blink | 311 } // namespace blink |
| OLD | NEW |