| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/frame/ImageBitmap.h" | 5 #include "core/frame/ImageBitmap.h" |
| 6 | 6 |
| 7 #include "core/html/Float32ImageData.h" | 7 #include "core/html/Float32ImageData.h" |
| 8 #include "core/html/HTMLCanvasElement.h" | 8 #include "core/html/HTMLCanvasElement.h" |
| 9 #include "core/html/HTMLVideoElement.h" | 9 #include "core/html/HTMLVideoElement.h" |
| 10 #include "core/html/ImageData.h" | 10 #include "core/html/ImageData.h" |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 info.bytesPerPixel()); | 465 info.bytesPerPixel()); |
| 466 if (!dstBuffer) | 466 if (!dstBuffer) |
| 467 return nullptr; | 467 return nullptr; |
| 468 RefPtr<Uint8Array> dstPixels = | 468 RefPtr<Uint8Array> dstPixels = |
| 469 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); | 469 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); |
| 470 return StaticBitmapImage::create(newSkImageFromRaster( | 470 return StaticBitmapImage::create(newSkImageFromRaster( |
| 471 info, std::move(dstPixels), | 471 info, std::move(dstPixels), |
| 472 static_cast<unsigned>(info.width()) * info.bytesPerPixel())); | 472 static_cast<unsigned>(info.width()) * info.bytesPerPixel())); |
| 473 } | 473 } |
| 474 | 474 |
| 475 // TODO(ccameron): Canvas should operate in sRGB and not display space. | 475 sk_sp<SkImage> skiaImage = image->imageForCurrentFrame(); |
| 476 // https://crbug.com/667431 | |
| 477 sk_sp<SkImage> skiaImage = | |
| 478 image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); | |
| 479 // Attempt to get raw unpremultiplied image data, executed only when skiaImage | 476 // Attempt to get raw unpremultiplied image data, executed only when skiaImage |
| 480 // is premultiplied. | 477 // is premultiplied. |
| 481 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || | 478 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || |
| 482 !skiaImage) && | 479 !skiaImage) && |
| 483 image->data() && imageFormat == PremultiplyAlpha) || | 480 image->data() && imageFormat == PremultiplyAlpha) || |
| 484 colorBehavior.isIgnore()) { | 481 colorBehavior.isIgnore()) { |
| 485 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( | 482 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( |
| 486 image->data(), true, | 483 image->data(), true, |
| 487 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied | 484 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied |
| 488 : ImageDecoder::AlphaNotPremultiplied, | 485 : ImageDecoder::AlphaNotPremultiplied, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 } else { | 568 } else { |
| 572 m_image = cropImageAndApplyColorSpaceConversion( | 569 m_image = cropImageAndApplyColorSpaceConversion( |
| 573 input.get(), parsedOptions, PremultiplyAlpha, | 570 input.get(), parsedOptions, PremultiplyAlpha, |
| 574 ColorBehavior::transformToGlobalTarget()); | 571 ColorBehavior::transformToGlobalTarget()); |
| 575 } | 572 } |
| 576 | 573 |
| 577 if (!m_image) | 574 if (!m_image) |
| 578 return; | 575 return; |
| 579 // In the case where the source image is lazy-decoded, m_image may not be in | 576 // In the case where the source image is lazy-decoded, m_image may not be in |
| 580 // a decoded state, we trigger it here. | 577 // a decoded state, we trigger it here. |
| 581 // TODO(ccameron): Canvas should operate in sRGB and not display space. | 578 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); |
| 582 // https://crbug.com/667431 | |
| 583 sk_sp<SkImage> skImage = | |
| 584 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()); | |
| 585 SkPixmap pixmap; | 579 SkPixmap pixmap; |
| 586 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { | 580 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { |
| 587 SkImageInfo imageInfo = SkImageInfo::Make( | 581 SkImageInfo imageInfo = SkImageInfo::Make( |
| 588 skImage->width(), skImage->height(), parsedOptions.dstColorType, | 582 skImage->width(), skImage->height(), parsedOptions.dstColorType, |
| 589 kPremul_SkAlphaType, parsedOptions.dstColorSpace); | 583 kPremul_SkAlphaType, parsedOptions.dstColorSpace); |
| 590 sk_sp<SkSurface> surface = SkSurface::MakeRaster(imageInfo); | 584 sk_sp<SkSurface> surface = SkSurface::MakeRaster(imageInfo); |
| 591 surface->getCanvas()->drawImage(skImage, 0, 0); | 585 surface->getCanvas()->drawImage(skImage, 0, 0); |
| 592 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); | 586 m_image = StaticBitmapImage::create(surface->makeImageSnapshot()); |
| 593 } | 587 } |
| 594 if (!m_image) | 588 if (!m_image) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 parsedOptions.premultiplyAlpha = true; | 664 parsedOptions.premultiplyAlpha = true; |
| 671 isPremultiplyAlphaReverted = true; | 665 isPremultiplyAlphaReverted = true; |
| 672 } | 666 } |
| 673 m_image = cropImageAndApplyColorSpaceConversion( | 667 m_image = cropImageAndApplyColorSpaceConversion( |
| 674 input.get(), parsedOptions, PremultiplyAlpha, | 668 input.get(), parsedOptions, PremultiplyAlpha, |
| 675 ColorBehavior::transformToGlobalTarget()); | 669 ColorBehavior::transformToGlobalTarget()); |
| 676 if (!m_image) | 670 if (!m_image) |
| 677 return; | 671 return; |
| 678 if (isPremultiplyAlphaReverted) { | 672 if (isPremultiplyAlphaReverted) { |
| 679 parsedOptions.premultiplyAlpha = false; | 673 parsedOptions.premultiplyAlpha = false; |
| 680 // TODO(ccameron): Canvas should operate in sRGB and not display space. | 674 m_image = StaticBitmapImage::create( |
| 681 // https://crbug.com/667431 | 675 premulSkImageToUnPremul(m_image->imageForCurrentFrame().get())); |
| 682 m_image = StaticBitmapImage::create(premulSkImageToUnPremul( | |
| 683 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) | |
| 684 .get())); | |
| 685 } | 676 } |
| 686 if (!m_image) | 677 if (!m_image) |
| 687 return; | 678 return; |
| 688 m_image->setOriginClean(canvas->originClean()); | 679 m_image->setOriginClean(canvas->originClean()); |
| 689 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); | 680 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); |
| 690 } | 681 } |
| 691 | 682 |
| 692 ImageBitmap::ImageBitmap(OffscreenCanvas* offscreenCanvas, | 683 ImageBitmap::ImageBitmap(OffscreenCanvas* offscreenCanvas, |
| 693 Optional<IntRect> cropRect, | 684 Optional<IntRect> cropRect, |
| 694 const ImageBitmapOptions& options) { | 685 const ImageBitmapOptions& options) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 708 parsedOptions.premultiplyAlpha = true; | 699 parsedOptions.premultiplyAlpha = true; |
| 709 isPremultiplyAlphaReverted = true; | 700 isPremultiplyAlphaReverted = true; |
| 710 } | 701 } |
| 711 m_image = cropImageAndApplyColorSpaceConversion( | 702 m_image = cropImageAndApplyColorSpaceConversion( |
| 712 input.get(), parsedOptions, PremultiplyAlpha, | 703 input.get(), parsedOptions, PremultiplyAlpha, |
| 713 ColorBehavior::transformToGlobalTarget()); | 704 ColorBehavior::transformToGlobalTarget()); |
| 714 if (!m_image) | 705 if (!m_image) |
| 715 return; | 706 return; |
| 716 if (isPremultiplyAlphaReverted) { | 707 if (isPremultiplyAlphaReverted) { |
| 717 parsedOptions.premultiplyAlpha = false; | 708 parsedOptions.premultiplyAlpha = false; |
| 718 m_image = StaticBitmapImage::create(premulSkImageToUnPremul( | 709 m_image = StaticBitmapImage::create( |
| 719 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) | 710 premulSkImageToUnPremul(m_image->imageForCurrentFrame().get())); |
| 720 .get())); | |
| 721 } | 711 } |
| 722 if (!m_image) | 712 if (!m_image) |
| 723 return; | 713 return; |
| 724 m_image->setOriginClean(offscreenCanvas->originClean()); | 714 m_image->setOriginClean(offscreenCanvas->originClean()); |
| 725 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); | 715 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); |
| 726 } | 716 } |
| 727 | 717 |
| 728 ImageBitmap::ImageBitmap(const void* pixelData, | 718 ImageBitmap::ImageBitmap(const void* pixelData, |
| 729 uint32_t width, | 719 uint32_t width, |
| 730 uint32_t height, | 720 uint32_t height, |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1074 return ImageBitmap::create(StaticBitmapImage::create(std::move(image))); | 1064 return ImageBitmap::create(StaticBitmapImage::create(std::move(image))); |
| 1075 } | 1065 } |
| 1076 | 1066 |
| 1077 PassRefPtr<Uint8Array> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp, | 1067 PassRefPtr<Uint8Array> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp, |
| 1078 DataColorFormat format) { | 1068 DataColorFormat format) { |
| 1079 SkImageInfo info = SkImageInfo::Make( | 1069 SkImageInfo info = SkImageInfo::Make( |
| 1080 width(), height(), | 1070 width(), height(), |
| 1081 (format == RGBAColorType) ? kRGBA_8888_SkColorType : kN32_SkColorType, | 1071 (format == RGBAColorType) ? kRGBA_8888_SkColorType : kN32_SkColorType, |
| 1082 (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType | 1072 (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType |
| 1083 : kUnpremul_SkAlphaType); | 1073 : kUnpremul_SkAlphaType); |
| 1084 // TODO(ccameron): Canvas should operate in sRGB and not display space. | 1074 RefPtr<Uint8Array> dstPixels = |
| 1085 // https://crbug.com/667431 | 1075 copySkImageData(m_image->imageForCurrentFrame().get(), info); |
| 1086 RefPtr<Uint8Array> dstPixels = copySkImageData( | |
| 1087 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) | |
| 1088 .get(), | |
| 1089 info); | |
| 1090 return dstPixels.release(); | 1076 return dstPixels.release(); |
| 1091 } | 1077 } |
| 1092 | 1078 |
| 1093 unsigned long ImageBitmap::width() const { | 1079 unsigned long ImageBitmap::width() const { |
| 1094 if (!m_image) | 1080 if (!m_image) |
| 1095 return 0; | 1081 return 0; |
| 1096 ASSERT(m_image->width() > 0); | 1082 ASSERT(m_image->width() > 0); |
| 1097 return m_image->width(); | 1083 return m_image->width(); |
| 1098 } | 1084 } |
| 1099 | 1085 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1136 AccelerationHint, | 1122 AccelerationHint, |
| 1137 SnapshotReason, | 1123 SnapshotReason, |
| 1138 const FloatSize&) const { | 1124 const FloatSize&) const { |
| 1139 *status = NormalSourceImageStatus; | 1125 *status = NormalSourceImageStatus; |
| 1140 if (!m_image) | 1126 if (!m_image) |
| 1141 return nullptr; | 1127 return nullptr; |
| 1142 if (m_image->isPremultiplied()) | 1128 if (m_image->isPremultiplied()) |
| 1143 return m_image; | 1129 return m_image; |
| 1144 // Skia does not support drawing unpremul SkImage on SkCanvas. | 1130 // Skia does not support drawing unpremul SkImage on SkCanvas. |
| 1145 // Premultiply and return. | 1131 // Premultiply and return. |
| 1146 sk_sp<SkImage> premulSkImage = unPremulSkImageToPremul( | 1132 sk_sp<SkImage> premulSkImage = |
| 1147 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) | 1133 unPremulSkImageToPremul(m_image->imageForCurrentFrame().get()); |
| 1148 .get()); | |
| 1149 return StaticBitmapImage::create(premulSkImage); | 1134 return StaticBitmapImage::create(premulSkImage); |
| 1150 } | 1135 } |
| 1151 | 1136 |
| 1152 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, | 1137 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, |
| 1153 FloatRect* dstRect) const {} | 1138 FloatRect* dstRect) const {} |
| 1154 | 1139 |
| 1155 FloatSize ImageBitmap::elementSize(const FloatSize&) const { | 1140 FloatSize ImageBitmap::elementSize(const FloatSize&) const { |
| 1156 return FloatSize(width(), height()); | 1141 return FloatSize(width(), height()); |
| 1157 } | 1142 } |
| 1158 | 1143 |
| 1159 DEFINE_TRACE(ImageBitmap) {} | 1144 DEFINE_TRACE(ImageBitmap) {} |
| 1160 | 1145 |
| 1161 } // namespace blink | 1146 } // namespace blink |
| OLD | NEW |