| 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 "third_party/skia/include/core/SkSurface.h" | 10 #include "third_party/skia/include/core/SkSurface.h" |
| 11 #include "wtf/RefPtr.h" | 11 #include "wtf/RefPtr.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 static inline IntRect normalizeRect(const IntRect& rect) | 15 static inline IntRect normalizeRect(const IntRect& rect) |
| 16 { | 16 { |
| 17 return IntRect(std::min(rect.x(), rect.maxX()), | 17 return IntRect(std::min(rect.x(), rect.maxX()), |
| 18 std::min(rect.y(), rect.maxY()), | 18 std::min(rect.y(), rect.maxY()), |
| 19 std::max(rect.width(), -rect.width()), | 19 std::max(rect.width(), -rect.width()), |
| 20 std::max(rect.height(), -rect.height())); | 20 std::max(rect.height(), -rect.height())); |
| 21 } | 21 } |
| 22 | 22 |
| 23 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop
Rect) | 23 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop
Rect, bool isOriginClean) |
| 24 { | 24 { |
| 25 ASSERT(image); | 25 ASSERT(image); |
| 26 | 26 |
| 27 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 27 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 28 const IntRect srcRect = intersection(imgRect, cropRect); | 28 const IntRect srcRect = intersection(imgRect, cropRect); |
| 29 | 29 |
| 30 if (cropRect == srcRect) | 30 if (cropRect == srcRect) |
| 31 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()-
>newSubset(srcRect))); | 31 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()-
>newSubset(srcRect)), isOriginClean ? HasSingleSecurityOrigin : NoSingleSecurity
Origin); |
| 32 | 32 |
| 33 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); | 33 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); |
| 34 | 34 |
| 35 if (srcRect.isEmpty()) | 35 if (srcRect.isEmpty()) |
| 36 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 36 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()),
isOriginClean ? HasSingleSecurityOrigin : NoSingleSecurityOrigin); |
| 37 | 37 |
| 38 SkScalar dstLeft = std::min(0, -cropRect.x()); | 38 SkScalar dstLeft = std::min(0, -cropRect.x()); |
| 39 SkScalar dstTop = std::min(0, -cropRect.y()); | 39 SkScalar dstTop = std::min(0, -cropRect.y()); |
| 40 if (cropRect.x() < 0) | 40 if (cropRect.x() < 0) |
| 41 dstLeft = -cropRect.x(); | 41 dstLeft = -cropRect.x(); |
| 42 if (cropRect.y() < 0) | 42 if (cropRect.y() < 0) |
| 43 dstTop = -cropRect.y(); | 43 dstTop = -cropRect.y(); |
| 44 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft
, dstTop); | 44 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft
, dstTop); |
| 45 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 45 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot()), isOr
iginClean ? HasSingleSecurityOrigin : NoSingleSecurityOrigin); |
| 46 } | 46 } |
| 47 | 47 |
| 48 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 48 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum
ent* document) |
| 49 { | 49 { |
| 50 m_image = cropImage(image->cachedImage()->image(), cropRect); | 50 m_image = cropImage(image->cachedImage()->image(), cropRect, !image->wouldTa
intOrigin(document->securityOrigin())); |
| 51 } | 51 } |
| 52 | 52 |
| 53 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 53 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum
ent* document) |
| 54 { | 54 { |
| 55 IntSize playerSize; | 55 IntSize playerSize; |
| 56 if (video->webMediaPlayer()) | 56 if (video->webMediaPlayer()) |
| 57 playerSize = video->webMediaPlayer()->naturalSize(); | 57 playerSize = video->webMediaPlayer()->naturalSize(); |
| 58 | 58 |
| 59 IntRect videoRect = IntRect(IntPoint(), playerSize); | 59 IntRect videoRect = IntRect(IntPoint(), playerSize); |
| 60 IntRect srcRect = intersection(cropRect, videoRect); | 60 IntRect srcRect = intersection(cropRect, videoRect); |
| 61 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 61 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 62 if (!buffer) | 62 if (!buffer) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); | 65 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); |
| 66 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); | 66 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); |
| 67 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration)); | 67 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration), !video->wouldTaintOrigin(document->securityOrigin()) ? HasSingleSecuri
tyOrigin : NoSingleSecurityOrigin); |
| 68 } | 68 } |
| 69 | 69 |
| 70 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 70 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
| 71 { | 71 { |
| 72 ASSERT(canvas->isPaintable()); | 72 ASSERT(canvas->isPaintable()); |
| 73 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get(
), cropRect); | 73 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get(
), cropRect, canvas->originClean()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 76 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 77 { | 77 { |
| 78 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 78 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 79 | 79 |
| 80 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 80 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 81 if (!buffer) | 81 if (!buffer) |
| 82 return; | 82 return; |
| 83 | 83 |
| 84 if (srcRect.isEmpty()) { | 84 if (srcRect.isEmpty()) { |
| 85 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA
cceleration)); | 85 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA
cceleration), HasSingleSecurityOrigin); |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 | 88 |
| 89 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); | 89 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); |
| 90 if (cropRect.x() < 0) | 90 if (cropRect.x() < 0) |
| 91 dstPoint.setX(-cropRect.x()); | 91 dstPoint.setX(-cropRect.x()); |
| 92 if (cropRect.y() < 0) | 92 if (cropRect.y() < 0) |
| 93 dstPoint.setY(-cropRect.y()); | 93 dstPoint.setY(-cropRect.y()); |
| 94 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); | 94 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); |
| 95 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration)); | 95 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel
eration), HasSingleSecurityOrigin); |
| 96 } | 96 } |
| 97 | 97 |
| 98 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 98 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 99 { | 99 { |
| 100 m_image = cropImage(bitmap->bitmapImage(), cropRect); | 100 m_image = cropImage(bitmap->bitmapImage(), cropRect, bitmap->originClean()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 103 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
| 104 { | 104 { |
| 105 m_image = cropImage(image, cropRect); | 105 m_image = cropImage(image, cropRect, image->currentFrameHasSingleSecurityOri
gin()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) | 108 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) |
| 109 { | 109 { |
| 110 m_image = image; | 110 m_image = image; |
| 111 } | 111 } |
| 112 | 112 |
| 113 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() | 113 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() |
| 114 { | 114 { |
| 115 ASSERT(!isNeutered()); | 115 ASSERT(!isNeutered()); |
| 116 m_isNeutered = true; | 116 m_isNeutered = true; |
| 117 return m_image.release(); | 117 return m_image.release(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 ImageBitmap::~ImageBitmap() | 120 ImageBitmap::~ImageBitmap() |
| 121 { | 121 { |
| 122 } | 122 } |
| 123 | 123 |
| 124 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect) | 124 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect, Document* document) |
| 125 { | 125 { |
| 126 IntRect normalizedCropRect = normalizeRect(cropRect); | 126 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 127 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 127 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect, documen
t)); |
| 128 } | 128 } |
| 129 | 129 |
| 130 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video,
const IntRect& cropRect) | 130 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video,
const IntRect& cropRect, Document* document) |
| 131 { | 131 { |
| 132 IntRect normalizedCropRect = normalizeRect(cropRect); | 132 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 133 return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect)); | 133 return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect, documen
t)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canva
s, const IntRect& cropRect) | 136 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canva
s, const IntRect& cropRect) |
| 137 { | 137 { |
| 138 IntRect normalizedCropRect = normalizeRect(cropRect); | 138 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 139 return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect)); | 139 return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const I
ntRect& cropRect) | 142 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const I
ntRect& cropRect) |
| 143 { | 143 { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 { | 213 { |
| 214 return FloatSize(width(), height()); | 214 return FloatSize(width(), height()); |
| 215 } | 215 } |
| 216 | 216 |
| 217 DEFINE_TRACE(ImageBitmap) | 217 DEFINE_TRACE(ImageBitmap) |
| 218 { | 218 { |
| 219 ImageLoaderClient::trace(visitor); | 219 ImageLoaderClient::trace(visitor); |
| 220 } | 220 } |
| 221 | 221 |
| 222 } // namespace blink | 222 } // namespace blink |
| OLD | NEW |