| 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/frame/ImageBitmap.h" | 6 #include "core/frame/ImageBitmap.h" |
| 7 | 7 |
| 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" |
| 11 #include "platform/graphics/GraphicsContext.h" | |
| 12 #include "platform/graphics/ImageBuffer.h" | |
| 13 #include "platform/graphics/StaticBitmapImage.h" | |
| 14 #include "platform/graphics/paint/DrawingRecorder.h" | |
| 15 #include "platform/graphics/paint/SkPictureBuilder.h" | |
| 16 #include "third_party/skia/include/core/SkScalar.h" | |
| 17 #include "third_party/skia/include/core/SkSurface.h" | 11 #include "third_party/skia/include/core/SkSurface.h" |
| 18 #include "wtf/RefPtr.h" | 12 #include "wtf/RefPtr.h" |
| 19 | 13 |
| 20 namespace blink { | 14 namespace blink { |
| 21 | 15 |
| 22 static inline IntRect normalizeRect(const IntRect& rect) | 16 static inline IntRect normalizeRect(const IntRect& rect) |
| 23 { | 17 { |
| 24 return IntRect(std::min(rect.x(), rect.maxX()), | 18 return IntRect(std::min(rect.x(), rect.maxX()), |
| 25 std::min(rect.y(), rect.maxY()), | 19 std::min(rect.y(), rect.maxY()), |
| 26 std::max(rect.width(), -rect.width()), | 20 std::max(rect.width(), -rect.width()), |
| 27 std::max(rect.height(), -rect.height())); | 21 std::max(rect.height(), -rect.height())); |
| 28 } | 22 } |
| 29 | 23 |
| 30 static PassRefPtr<SkImage> cropImage(PassRefPtr<SkImage> image, const IntRect& c
ropRect) | 24 static PassRefPtr<StaticBitmapImage> cropImage(StaticBitmapImage* image, const I
ntRect& cropRect) |
| 31 { | 25 { |
| 32 ASSERT(image); | 26 ASSERT(image); |
| 33 | 27 |
| 34 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 28 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 35 const IntRect srcRect = intersection(imgRect, cropRect); | 29 const IntRect srcRect = intersection(imgRect, cropRect); |
| 36 | 30 |
| 37 if (cropRect == srcRect) | 31 if (cropRect == srcRect) |
| 38 return adoptRef(image->newSubset(srcRect)); | 32 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()-
>newSubset(srcRect))); |
| 39 | 33 |
| 40 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); | 34 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); |
| 41 | 35 |
| 42 if (srcRect.isEmpty()) | 36 if (srcRect.isEmpty()) |
| 43 return adoptRef(surface->newImageSnapshot()); | 37 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
| 44 | 38 |
| 45 SkScalar dstLeft = std::min(0, -cropRect.x()); | 39 SkScalar dstLeft = std::min(0, -cropRect.x()); |
| 46 SkScalar dstTop = std::min(0, -cropRect.y()); | 40 SkScalar dstTop = std::min(0, -cropRect.y()); |
| 47 if (cropRect.x() < 0) | 41 if (cropRect.x() < 0) |
| 48 dstLeft = -cropRect.x(); | 42 dstLeft = -cropRect.x(); |
| 49 if (cropRect.y() < 0) | 43 if (cropRect.y() < 0) |
| 50 dstTop = -cropRect.y(); | 44 dstTop = -cropRect.y(); |
| 51 surface->getCanvas()->drawImage(image.get(), dstLeft, dstTop); | 45 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft
, dstTop); |
| 52 return adoptRef(surface->newImageSnapshot()); | 46 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
| 53 } | 47 } |
| 54 | 48 |
| 55 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 49 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 56 { | 50 { |
| 57 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c
ropRect); | 51 m_image = cropImage(static_cast<StaticBitmapImage*>(image->cachedImage()->im
age()), cropRect); |
| 58 } | 52 } |
| 59 | 53 |
| 60 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 54 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
| 61 { | 55 { |
| 62 IntSize playerSize; | 56 IntSize playerSize; |
| 63 if (video->webMediaPlayer()) | 57 if (video->webMediaPlayer()) |
| 64 playerSize = video->webMediaPlayer()->naturalSize(); | 58 playerSize = video->webMediaPlayer()->naturalSize(); |
| 65 | 59 |
| 66 IntRect videoRect = IntRect(IntPoint(), playerSize); | 60 IntRect videoRect = IntRect(IntPoint(), playerSize); |
| 67 IntRect srcRect = intersection(cropRect, videoRect); | 61 IntRect srcRect = intersection(cropRect, videoRect); |
| 68 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 69 if (!buffer) | 63 if (!buffer) |
| 70 return; | 64 return; |
| 71 | 65 |
| 72 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); | 66 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); |
| 73 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); | 67 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); |
| 74 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 68 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration)); |
| 75 } | 69 } |
| 76 | 70 |
| 77 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 71 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
| 78 { | 72 { |
| 79 ASSERT(canvas->isPaintable()); | 73 ASSERT(canvas->isPaintable()); |
| 80 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima
geForCurrentFrame(), cropRect); | 74 m_image = cropImage(static_cast<StaticBitmapImage*>(canvas->copiedImage(Back
Buffer, PreferAcceleration).get()), cropRect); |
| 81 } | 75 } |
| 82 | 76 |
| 83 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 77 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 84 { | 78 { |
| 85 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 79 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 86 | 80 |
| 87 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 81 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 88 if (!buffer) | 82 if (!buffer) |
| 89 return; | 83 return; |
| 90 | 84 |
| 91 if (srcRect.isEmpty()) { | 85 if (srcRect.isEmpty()) { |
| 92 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 86 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA
cceleration)); |
| 93 return; | 87 return; |
| 94 } | 88 } |
| 95 | 89 |
| 96 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); | 90 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); |
| 97 if (cropRect.x() < 0) | 91 if (cropRect.x() < 0) |
| 98 dstPoint.setX(-cropRect.x()); | 92 dstPoint.setX(-cropRect.x()); |
| 99 if (cropRect.y() < 0) | 93 if (cropRect.y() < 0) |
| 100 dstPoint.setY(-cropRect.y()); | 94 dstPoint.setY(-cropRect.y()); |
| 101 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); | 95 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); |
| 102 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 96 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration)); |
| 103 } | 97 } |
| 104 | 98 |
| 105 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 99 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 106 { | 100 { |
| 107 m_image = cropImage(bitmap->skImage(), cropRect); | 101 m_image = cropImage(bitmap->bitmapImage(), cropRect); |
| 108 } | 102 } |
| 109 | 103 |
| 110 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 104 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
| 111 { | 105 { |
| 112 m_image = cropImage(image->imageForCurrentFrame(), cropRect); | 106 m_image = cropImage(static_cast<StaticBitmapImage*>(image), cropRect); |
| 113 } | 107 } |
| 114 | 108 |
| 115 ImageBitmap::~ImageBitmap() | 109 ImageBitmap::~ImageBitmap() |
| 116 { | 110 { |
| 117 } | 111 } |
| 118 | 112 |
| 119 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect) | 113 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect) |
| 120 { | 114 { |
| 121 IntRect normalizedCropRect = normalizeRect(cropRect); | 115 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 122 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 116 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 return IntSize(m_image->width(), m_image->height()); | 170 return IntSize(m_image->width(), m_image->height()); |
| 177 } | 171 } |
| 178 | 172 |
| 179 void ImageBitmap::notifyImageSourceChanged() | 173 void ImageBitmap::notifyImageSourceChanged() |
| 180 { | 174 { |
| 181 } | 175 } |
| 182 | 176 |
| 183 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status
, AccelerationHint) const | 177 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status
, AccelerationHint) const |
| 184 { | 178 { |
| 185 *status = NormalSourceImageStatus; | 179 *status = NormalSourceImageStatus; |
| 186 return m_image ? StaticBitmapImage::create(m_image) : nullptr; | 180 return m_image ? m_image : nullptr; |
| 187 } | 181 } |
| 188 | 182 |
| 189 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const | 183 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const |
| 190 { | 184 { |
| 191 } | 185 } |
| 192 | 186 |
| 193 FloatSize ImageBitmap::elementSize() const | 187 FloatSize ImageBitmap::elementSize() const |
| 194 { | 188 { |
| 195 return FloatSize(width(), height()); | 189 return FloatSize(width(), height()); |
| 196 } | 190 } |
| 197 | 191 |
| 198 DEFINE_TRACE(ImageBitmap) | 192 DEFINE_TRACE(ImageBitmap) |
| 199 { | 193 { |
| 200 ImageLoaderClient::trace(visitor); | 194 ImageLoaderClient::trace(visitor); |
| 201 } | 195 } |
| 202 | 196 |
| 203 } // namespace blink | 197 } // namespace blink |
| OLD | NEW |