| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 { | 23 { |
| 24 return IntRect(std::min(rect.x(), rect.maxX()), | 24 return IntRect(std::min(rect.x(), rect.maxX()), |
| 25 std::min(rect.y(), rect.maxY()), | 25 std::min(rect.y(), rect.maxY()), |
| 26 std::max(rect.width(), -rect.width()), | 26 std::max(rect.width(), -rect.width()), |
| 27 std::max(rect.height(), -rect.height())); | 27 std::max(rect.height(), -rect.height())); |
| 28 } | 28 } |
| 29 | 29 |
| 30 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 30 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 31 { | 31 { |
| 32 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c
ropRect); | 32 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c
ropRect); |
| 33 m_isNeutered = false; |
| 33 } | 34 } |
| 34 | 35 |
| 35 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 36 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
| 36 { | 37 { |
| 37 IntSize playerSize; | 38 IntSize playerSize; |
| 38 if (video->webMediaPlayer()) | 39 if (video->webMediaPlayer()) |
| 39 playerSize = video->webMediaPlayer()->naturalSize(); | 40 playerSize = video->webMediaPlayer()->naturalSize(); |
| 40 | 41 |
| 41 IntRect videoRect = IntRect(IntPoint(), playerSize); | 42 IntRect videoRect = IntRect(IntPoint(), playerSize); |
| 42 IntRect srcRect = intersection(cropRect, videoRect); | 43 IntRect srcRect = intersection(cropRect, videoRect); |
| 43 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 44 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 44 if (!buffer) | 45 if (!buffer) |
| 45 return; | 46 return; |
| 46 | 47 |
| 47 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); | 48 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe
ct.y())); |
| 48 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); | 49 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size())
, nullptr); |
| 49 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 50 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); |
| 51 m_isNeutered = false; |
| 50 } | 52 } |
| 51 | 53 |
| 52 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 54 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
| 53 { | 55 { |
| 54 ASSERT(canvas->isPaintable()); | 56 ASSERT(canvas->isPaintable()); |
| 55 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima
geForCurrentFrame(), cropRect); | 57 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima
geForCurrentFrame(), cropRect); |
| 58 m_isNeutered = false; |
| 56 } | 59 } |
| 57 | 60 |
| 58 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 61 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 59 { | 62 { |
| 60 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 63 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 61 | 64 |
| 62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); | 65 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque,
DoNotInitializeImagePixels); |
| 63 if (!buffer) | 66 if (!buffer) |
| 64 return; | 67 return; |
| 65 | 68 |
| 66 if (srcRect.isEmpty()) { | 69 if (srcRect.isEmpty()) { |
| 67 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 70 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); |
| 68 return; | 71 return; |
| 69 } | 72 } |
| 70 | 73 |
| 71 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); | 74 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe
ct.y())); |
| 72 if (cropRect.x() < 0) | 75 if (cropRect.x() < 0) |
| 73 dstPoint.setX(-cropRect.x()); | 76 dstPoint.setX(-cropRect.x()); |
| 74 if (cropRect.y() < 0) | 77 if (cropRect.y() < 0) |
| 75 dstPoint.setY(-cropRect.y()); | 78 dstPoint.setY(-cropRect.y()); |
| 76 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); | 79 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe
ct, dstPoint); |
| 77 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | 80 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); |
| 81 m_isNeutered = false; |
| 78 } | 82 } |
| 79 | 83 |
| 80 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 84 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 81 { | 85 { |
| 82 m_image = cropImage(bitmap->skImage(), cropRect); | 86 m_image = cropImage(bitmap->skImage(), cropRect); |
| 87 m_isNeutered = false; |
| 83 } | 88 } |
| 84 | 89 |
| 85 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 90 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
| 86 { | 91 { |
| 87 m_image = cropImage(image->imageForCurrentFrame(), cropRect); | 92 m_image = cropImage(image->imageForCurrentFrame(), cropRect); |
| 93 m_isNeutered = false; |
| 94 } |
| 95 |
| 96 ImageBitmap::ImageBitmap(PassRefPtr<SkImage> image) |
| 97 { |
| 98 m_image = image; |
| 99 m_isNeutered = false; |
| 100 } |
| 101 |
| 102 PassRefPtr<SkImage> ImageBitmap::transfer() |
| 103 { |
| 104 ASSERT(!isNeutered()); |
| 105 m_isNeutered = true; |
| 106 return m_image.release(); |
| 88 } | 107 } |
| 89 | 108 |
| 90 ImageBitmap::~ImageBitmap() | 109 ImageBitmap::~ImageBitmap() |
| 91 { | 110 { |
| 92 } | 111 } |
| 93 | 112 |
| 94 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect) | 113 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image,
const IntRect& cropRect) |
| 95 { | 114 { |
| 96 IntRect normalizedCropRect = normalizeRect(cropRect); | 115 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 97 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 116 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 120 IntRect normalizedCropRect = normalizeRect(cropRect); | 139 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 121 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); | 140 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); |
| 122 } | 141 } |
| 123 | 142 |
| 124 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR
ect& cropRect) | 143 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR
ect& cropRect) |
| 125 { | 144 { |
| 126 IntRect normalizedCropRect = normalizeRect(cropRect); | 145 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 127 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 146 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
| 128 } | 147 } |
| 129 | 148 |
| 149 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(PassRefPtr<SkImage> imag
e) |
| 150 { |
| 151 return adoptRefWillBeNoop(new ImageBitmap(image)); |
| 152 } |
| 153 |
| 130 PassRefPtr<SkImage> ImageBitmap::cropImage(PassRefPtr<SkImage> image, const IntR
ect& cropRect) | 154 PassRefPtr<SkImage> ImageBitmap::cropImage(PassRefPtr<SkImage> image, const IntR
ect& cropRect) |
| 131 { | 155 { |
| 132 ASSERT(image); | 156 ASSERT(image); |
| 133 | 157 |
| 134 IntRect imgRect = IntRect(IntPoint(), IntSize(image->width(), image->height(
))); | 158 IntRect imgRect = IntRect(IntPoint(), IntSize(image->width(), image->height(
))); |
| 135 const IntRect srcRect = intersection(imgRect, cropRect); | 159 const IntRect srcRect = intersection(imgRect, cropRect); |
| 136 | 160 |
| 137 if (cropRect == srcRect) | 161 if (cropRect == srcRect) |
| 138 return adoptRef(image->newSubset(srcRect)); | 162 return adoptRef(image->newSubset(srcRect)); |
| 139 | 163 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 { | 195 { |
| 172 return FloatSize(width(), height()); | 196 return FloatSize(width(), height()); |
| 173 } | 197 } |
| 174 | 198 |
| 175 DEFINE_TRACE(ImageBitmap) | 199 DEFINE_TRACE(ImageBitmap) |
| 176 { | 200 { |
| 177 ImageLoaderClient::trace(visitor); | 201 ImageLoaderClient::trace(visitor); |
| 178 } | 202 } |
| 179 | 203 |
| 180 } // namespace blink | 204 } // namespace blink |
| OLD | NEW |