Chromium Code Reviews| 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/HTMLCanvasElement.h" | 7 #include "core/html/HTMLCanvasElement.h" |
| 8 #include "core/html/HTMLVideoElement.h" | 8 #include "core/html/HTMLVideoElement.h" |
| 9 #include "core/html/ImageData.h" | 9 #include "core/html/ImageData.h" |
| 10 #include "platform/graphics/skia/SkiaUtils.h" | 10 #include "platform/graphics/skia/SkiaUtils.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 return newSkImageFromRaster(info, std::move(imagePixels), imageRowBytes); | 84 return newSkImageFromRaster(info, std::move(imagePixels), imageRowBytes); |
| 85 } | 85 } |
| 86 | 86 |
| 87 static PassRefPtr<SkImage> premulSkImageToUnPremul(SkImage* input) | 87 static PassRefPtr<SkImage> premulSkImageToUnPremul(SkImage* input) |
| 88 { | 88 { |
| 89 SkImageInfo info = SkImageInfo::Make(input->width(), input->height(), kN32_S kColorType, kUnpremul_SkAlphaType); | 89 SkImageInfo info = SkImageInfo::Make(input->width(), input->height(), kN32_S kColorType, kUnpremul_SkAlphaType); |
| 90 OwnPtr<uint8_t[]> dstPixels = copySkImageData(input, info); | 90 OwnPtr<uint8_t[]> dstPixels = copySkImageData(input, info); |
| 91 return newSkImageFromRaster(info, std::move(dstPixels), input->width() * inf o.bytesPerPixel()); | 91 return newSkImageFromRaster(info, std::move(dstPixels), input->width() * inf o.bytesPerPixel()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 static PassRefPtr<SkImage> unPremulSkImageToPremul(SkImage* input) | |
| 95 { | |
| 96 SkImageInfo info = SkImageInfo::Make(input->width(), input->height(), kN32_S kColorType, kPremul_SkAlphaType); | |
| 97 OwnPtr<uint8_t[]> dstPixels = copySkImageData(input, info); | |
| 98 return newSkImageFromRaster(info, std::move(dstPixels), input->width() * inf o.bytesPerPixel()); | |
| 99 } | |
| 100 | |
| 94 PassRefPtr<SkImage> ImageBitmap::getSkImageFromDecoder(PassOwnPtr<ImageDecoder> decoder) | 101 PassRefPtr<SkImage> ImageBitmap::getSkImageFromDecoder(PassOwnPtr<ImageDecoder> decoder) |
| 95 { | 102 { |
| 96 if (!decoder->frameCount()) | 103 if (!decoder->frameCount()) |
| 97 return nullptr; | 104 return nullptr; |
| 98 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 105 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 99 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) | 106 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) |
| 100 return nullptr; | 107 return nullptr; |
| 101 SkBitmap bitmap = frame->bitmap(); | 108 SkBitmap bitmap = frame->bitmap(); |
| 102 if (!frameIsValid(bitmap)) | 109 if (!frameIsValid(bitmap)) |
| 103 return nullptr; | 110 return nullptr; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 133 decoder->setData(image->data(), true); | 140 decoder->setData(image->data(), true); |
| 134 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); | 141 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); |
| 135 if (!skiaImage) | 142 if (!skiaImage) |
| 136 return nullptr; | 143 return nullptr; |
| 137 } | 144 } |
| 138 | 145 |
| 139 if (cropRect == srcRect) { | 146 if (cropRect == srcRect) { |
| 140 if (flipY) | 147 if (flipY) |
| 141 return StaticBitmapImage::create(flipSkImageVertically(skiaImage->ne wSubset(srcRect), premultiplyAlpha ? PremultiplyAlpha : DontPremultiplyAlpha)); | 148 return StaticBitmapImage::create(flipSkImageVertically(skiaImage->ne wSubset(srcRect), premultiplyAlpha ? PremultiplyAlpha : DontPremultiplyAlpha)); |
| 142 SkImage* croppedSkImage = skiaImage->newSubset(srcRect); | 149 SkImage* croppedSkImage = skiaImage->newSubset(srcRect); |
| 150 // Special case: The first parameter image is unpremul but we need to tu rn it into premul. | |
| 151 if (premultiplyAlpha && imageFormat == PremultiplyAlpha) | |
|
Justin Novosad
2016/05/27 14:16:18
Unless I am misreading this, I think you mean "pre
xidachen
2016/05/27 15:10:13
Made changes so that imageFormat == DontPremultipl
| |
| 152 return StaticBitmapImage::create(unPremulSkImageToPremul(croppedSkIm age)); | |
| 143 // Call preroll to trigger image decoding. | 153 // Call preroll to trigger image decoding. |
| 144 croppedSkImage->preroll(); | 154 croppedSkImage->preroll(); |
| 145 return StaticBitmapImage::create(adoptRef(croppedSkImage)); | 155 return StaticBitmapImage::create(adoptRef(croppedSkImage)); |
| 146 } | 156 } |
| 147 | 157 |
| 148 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(cropRect.width(), cropRect.height()); | 158 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(cropRect.width(), cropRect.height()); |
| 149 if (!surface) | 159 if (!surface) |
| 150 return nullptr; | 160 return nullptr; |
| 151 if (srcRect.isEmpty()) | 161 if (srcRect.isEmpty()) |
| 152 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 162 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
| 153 | 163 |
| 154 SkScalar dstLeft = std::min(0, -cropRect.x()); | 164 SkScalar dstLeft = std::min(0, -cropRect.x()); |
| 155 SkScalar dstTop = std::min(0, -cropRect.y()); | 165 SkScalar dstTop = std::min(0, -cropRect.y()); |
| 156 if (cropRect.x() < 0) | 166 if (cropRect.x() < 0) |
| 157 dstLeft = -cropRect.x(); | 167 dstLeft = -cropRect.x(); |
| 158 if (cropRect.y() < 0) | 168 if (cropRect.y() < 0) |
| 159 dstTop = -cropRect.y(); | 169 dstTop = -cropRect.y(); |
| 160 surface->getCanvas()->drawImage(skiaImage.get(), dstLeft, dstTop); | 170 surface->getCanvas()->drawImage(skiaImage.get(), dstLeft, dstTop); |
| 161 if (flipY) | 171 if (flipY) |
| 162 skiaImage = flipSkImageVertically(surface->newImageSnapshot(), Premultip lyAlpha); | 172 skiaImage = flipSkImageVertically(surface->newImageSnapshot(), Premultip lyAlpha); |
| 163 else | 173 else |
| 164 skiaImage = adoptRef(surface->newImageSnapshot()); | 174 skiaImage = adoptRef(surface->newImageSnapshot()); |
| 165 if (premultiplyAlpha) | 175 if (premultiplyAlpha) { |
| 176 if (imageFormat == PremultiplyAlpha) | |
| 177 return StaticBitmapImage::create(unPremulSkImageToPremul(skiaImage.g et())); | |
| 166 return StaticBitmapImage::create(skiaImage.release()); | 178 return StaticBitmapImage::create(skiaImage.release()); |
| 179 } | |
| 167 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); | 180 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); |
| 168 } | 181 } |
| 169 | 182 |
| 170 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 183 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
| 171 { | 184 { |
| 172 bool flipY; | 185 bool flipY; |
| 173 bool premultiplyAlpha; | 186 bool premultiplyAlpha; |
| 174 parseOptions(options, flipY, premultiplyAlpha); | 187 parseOptions(options, flipY, premultiplyAlpha); |
| 175 | 188 |
| 176 if (options.colorSpaceConversion() == "none") | 189 if (options.colorSpaceConversion() == "none") |
| 177 m_image = cropImage(image->cachedImage()->getImage(), cropRect, flipY, p remultiplyAlpha, DontPremultiplyAlpha, ImageDecoder::GammaAndColorProfileIgnored ); | 190 m_image = cropImage(image->cachedImage()->getImage(), cropRect, flipY, p remultiplyAlpha, DontPremultiplyAlpha, ImageDecoder::GammaAndColorProfileIgnored ); |
| 178 else | 191 else |
| 179 m_image = cropImage(image->cachedImage()->getImage(), cropRect, flipY, p remultiplyAlpha, DontPremultiplyAlpha, ImageDecoder::GammaAndColorProfileApplied ); | 192 m_image = cropImage(image->cachedImage()->getImage(), cropRect, flipY, p remultiplyAlpha, DontPremultiplyAlpha, ImageDecoder::GammaAndColorProfileApplied ); |
| 180 if (!m_image) | 193 if (!m_image) |
| 181 return; | 194 return; |
| 195 // In the case where the source image is lazy-decoded, m_image may not be in | |
| 196 // a decoded state, we trigger it here. | |
| 197 RefPtr<SkImage> skImage = m_image->imageForCurrentFrame(); | |
| 198 SkPixmap pixmap; | |
| 199 if (!skImage->peekPixels(&pixmap)) { | |
|
Justin Novosad
2016/05/27 14:16:18
This condition will force an immediate readback of
xidachen
2016/05/27 15:10:13
Double checked with fmalita@, peekPixels() will no
Justin Novosad
2016/05/27 16:13:47
That is not what I meant.
I was not referring to t
xidachen
2016/05/27 17:14:56
Yes, that makes sense, changed.
| |
| 200 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(skImage->width (), skImage->height()); | |
| 201 surface->getCanvas()->drawImage(skImage.get(), 0, 0); | |
| 202 m_image = StaticBitmapImage::create(adoptRef(surface->newImageSnapshot() )); | |
| 203 } | |
| 182 m_image->setOriginClean(!image->wouldTaintOrigin(document->getSecurityOrigin ())); | 204 m_image->setOriginClean(!image->wouldTaintOrigin(document->getSecurityOrigin ())); |
| 183 m_image->setPremultiplied(premultiplyAlpha); | 205 m_image->setPremultiplied(premultiplyAlpha); |
| 184 } | 206 } |
| 185 | 207 |
| 186 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 208 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
| 187 { | 209 { |
| 188 IntSize playerSize; | 210 IntSize playerSize; |
| 189 if (video->webMediaPlayer()) | 211 if (video->webMediaPlayer()) |
| 190 playerSize = video->webMediaPlayer()->naturalSize(); | 212 playerSize = video->webMediaPlayer()->naturalSize(); |
| 191 | 213 |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 FloatSize ImageBitmap::elementSize(const FloatSize&) const | 511 FloatSize ImageBitmap::elementSize(const FloatSize&) const |
| 490 { | 512 { |
| 491 return FloatSize(width(), height()); | 513 return FloatSize(width(), height()); |
| 492 } | 514 } |
| 493 | 515 |
| 494 DEFINE_TRACE(ImageBitmap) | 516 DEFINE_TRACE(ImageBitmap) |
| 495 { | 517 { |
| 496 } | 518 } |
| 497 | 519 |
| 498 } // namespace blink | 520 } // namespace blink |
| OLD | NEW |