Chromium Code Reviews| Index: third_party/WebKit/Source/core/frame/ImageBitmap.cpp |
| diff --git a/third_party/WebKit/Source/core/frame/ImageBitmap.cpp b/third_party/WebKit/Source/core/frame/ImageBitmap.cpp |
| index 7d29b5119ed66e8892953d1b7b26ca6414d0de65..87ee402b82329a8639ba01ab1f2334fe4f1b19ab 100644 |
| --- a/third_party/WebKit/Source/core/frame/ImageBitmap.cpp |
| +++ b/third_party/WebKit/Source/core/frame/ImageBitmap.cpp |
| @@ -363,6 +363,18 @@ ImageBitmap::ImageBitmap(std::unique_ptr<uint8_t[]> data, uint32_t width, uint32 |
| m_image->setOriginClean(isImageBitmapOriginClean); |
| } |
| +static PassRefPtr<SkImage> scaleSkImage(PassRefPtr<SkImage> skImage, unsigned resizeWidth, unsigned resizeHeight, SkFilterQuality resizeQuality) |
| +{ |
| + SkImageInfo resizedInfo = SkImageInfo::Make(resizeWidth, resizeHeight, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
|
Justin Novosad
2016/07/27 17:05:05
Why BGRA instead of N32 (the platform-specific def
xidachen
2016/07/28 13:21:16
Done, changed to N32. Swizzle is applied only when
|
| + std::unique_ptr<uint8_t[]> resizedPixels = wrapArrayUnique(new uint8_t[resizeWidth * resizeHeight * resizedInfo.bytesPerPixel()]); |
| + SkPixmap pixmap(resizedInfo, resizedPixels.release(), resizeWidth * resizedInfo.bytesPerPixel()); |
| + skImage->scalePixels(pixmap, resizeQuality); |
| + return fromSkSp(SkImage::MakeFromRaster(pixmap, [](const void* pixels, void*) |
| + { |
| + delete[] static_cast<const uint8_t*>(pixels); |
| + }, nullptr)); |
| +} |
| + |
| ImageBitmap::ImageBitmap(ImageData* data, Optional<IntRect> cropRect, const ImageBitmapOptions& options) |
| { |
| // TODO(xidachen): implement the resize option |
| @@ -380,9 +392,10 @@ ImageBitmap::ImageBitmap(ImageData* data, Optional<IntRect> cropRect, const Imag |
| SkImageInfo info = SkImageInfo::Make(parsedOptions.cropRect.width(), dstHeight, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
| int srcPixelBytesPerRow = info.bytesPerPixel() * data->size().width(); |
| int dstPixelBytesPerRow = info.bytesPerPixel() * parsedOptions.cropRect.width(); |
| + RefPtr<SkImage> skImage; |
| if (parsedOptions.cropRect == IntRect(IntPoint(), data->size())) { |
| swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, parsedOptions.flipY); |
| - m_image = StaticBitmapImage::create(fromSkSp(SkImage::MakeRasterCopy(SkPixmap(info, srcAddr, dstPixelBytesPerRow)))); |
| + skImage = fromSkSp(SkImage::MakeRasterCopy(SkPixmap(info, srcAddr, dstPixelBytesPerRow))); |
|
Justin Novosad
2016/07/27 17:05:05
It's a shame to have to make an make a copy here t
xidachen
2016/07/28 13:21:16
Not easy. SkSurface doesn't support un-premultipli
|
| // restore the original ImageData |
| swizzleImageData(srcAddr, srcHeight, srcPixelBytesPerRow, parsedOptions.flipY); |
| } else { |
| @@ -414,8 +427,12 @@ ImageBitmap::ImageBitmap(ImageData* data, Optional<IntRect> cropRect, const Imag |
| } |
| } |
| } |
| - m_image = StaticBitmapImage::create(newSkImageFromRaster(info, std::move(copiedDataBuffer), dstPixelBytesPerRow)); |
| + skImage = newSkImageFromRaster(info, std::move(copiedDataBuffer), dstPixelBytesPerRow); |
| } |
| + if (parsedOptions.shouldScaleInput) |
| + m_image = StaticBitmapImage::create(scaleSkImage(skImage, parsedOptions.resizeWidth, parsedOptions.resizeHeight, parsedOptions.resizeQuality)); |
| + else |
| + m_image = StaticBitmapImage::create(skImage); |
| m_image->setPremultiplied(parsedOptions.premultiplyAlpha); |
| return; |
| } |
| @@ -435,10 +452,20 @@ ImageBitmap::ImageBitmap(ImageData* data, Optional<IntRect> cropRect, const Imag |
| if (parsedOptions.cropRect.y() < 0) |
| dstPoint.setY(-parsedOptions.cropRect.y()); |
| buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRect, dstPoint); |
| + RefPtr<SkImage> skImage = buffer->newSkImageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown); |
| if (parsedOptions.flipY) |
| - m_image = StaticBitmapImage::create(flipSkImageVertically(buffer->newSkImageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown).get(), PremultiplyAlpha)); |
| - else |
| - m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown)); |
| + skImage = flipSkImageVertically(skImage.get(), PremultiplyAlpha); |
| + if (parsedOptions.shouldScaleInput) { |
| + sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(parsedOptions.resizeWidth, parsedOptions.resizeHeight); |
| + if (!surface) |
| + return; |
| + SkPaint paint; |
| + paint.setFilterQuality(parsedOptions.resizeQuality); |
| + SkRect dstDrawRect = SkRect::MakeWH(parsedOptions.resizeWidth, parsedOptions.resizeHeight); |
| + surface->getCanvas()->drawImageRect(skImage.get(), dstDrawRect, &paint); |
| + skImage = fromSkSp(surface->makeImageSnapshot()); |
| + } |
| + m_image = StaticBitmapImage::create(skImage); |
| } |
| ImageBitmap::ImageBitmap(ImageBitmap* bitmap, Optional<IntRect> cropRect, const ImageBitmapOptions& options) |