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" | 11 #include "platform/graphics/GraphicsContext.h" |
12 #include "platform/graphics/ImageBuffer.h" | 12 #include "platform/graphics/ImageBuffer.h" |
13 #include "platform/graphics/StaticBitmapImage.h" | 13 #include "platform/graphics/StaticBitmapImage.h" |
14 #include "platform/graphics/paint/DrawingRecorder.h" | 14 #include "platform/graphics/paint/DrawingRecorder.h" |
15 #include "platform/graphics/paint/SkPictureBuilder.h" | 15 #include "platform/graphics/paint/SkPictureBuilder.h" |
16 #include "third_party/skia/include/core/SkScalar.h" | 16 #include "third_party/skia/include/core/SkScalar.h" |
17 #include "third_party/skia/include/core/SkSurface.h" | 17 #include "third_party/skia/include/core/SkSurface.h" |
18 #include "wtf/RefPtr.h" | 18 #include "wtf/RefPtr.h" |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 static inline IntRect normalizeRect(const IntRect& rect) | 22 static inline IntRect normalizeRect(const IntRect& rect) |
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 static PassRefPtr<SkImage> cropImage(PassRefPtr<SkImage> image, const IntRect& c
ropRect) |
| 31 { |
| 32 ASSERT(image); |
| 33 |
| 34 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 35 const IntRect srcRect = intersection(imgRect, cropRect); |
| 36 |
| 37 if (cropRect == srcRect) |
| 38 return adoptRef(image->newSubset(srcRect)); |
| 39 |
| 40 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); |
| 41 |
| 42 if (srcRect.isEmpty()) |
| 43 return adoptRef(surface->newImageSnapshot()); |
| 44 |
| 45 SkScalar dstLeft = std::min(0, -cropRect.x()); |
| 46 SkScalar dstTop = std::min(0, -cropRect.y()); |
| 47 if (cropRect.x() < 0) |
| 48 dstLeft = -cropRect.x(); |
| 49 if (cropRect.y() < 0) |
| 50 dstTop = -cropRect.y(); |
| 51 surface->getCanvas()->drawImage(image.get(), dstLeft, dstTop); |
| 52 return adoptRef(surface->newImageSnapshot()); |
| 53 } |
| 54 |
30 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 55 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
31 { | 56 { |
32 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c
ropRect); | 57 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c
ropRect); |
33 } | 58 } |
34 | 59 |
35 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 60 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
36 { | 61 { |
37 IntSize playerSize; | 62 IntSize playerSize; |
38 if (video->webMediaPlayer()) | 63 if (video->webMediaPlayer()) |
39 playerSize = video->webMediaPlayer()->naturalSize(); | 64 playerSize = video->webMediaPlayer()->naturalSize(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 IntRect normalizedCropRect = normalizeRect(cropRect); | 145 IntRect normalizedCropRect = normalizeRect(cropRect); |
121 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); | 146 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); |
122 } | 147 } |
123 | 148 |
124 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR
ect& cropRect) | 149 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR
ect& cropRect) |
125 { | 150 { |
126 IntRect normalizedCropRect = normalizeRect(cropRect); | 151 IntRect normalizedCropRect = normalizeRect(cropRect); |
127 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 152 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
128 } | 153 } |
129 | 154 |
130 PassRefPtr<SkImage> ImageBitmap::cropImage(PassRefPtr<SkImage> image, const IntR
ect& cropRect) | |
131 { | |
132 ASSERT(image); | |
133 | |
134 IntRect imgRect = IntRect(IntPoint(), IntSize(image->width(), image->height(
))); | |
135 const IntRect srcRect = intersection(imgRect, cropRect); | |
136 | |
137 if (cropRect == srcRect) | |
138 return adoptRef(image->newSubset(srcRect)); | |
139 | |
140 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.
width(), cropRect.height())); | |
141 | |
142 if (srcRect.isEmpty()) | |
143 return adoptRef(surface->newImageSnapshot()); | |
144 | |
145 SkScalar dstLeft = std::min(0, -cropRect.x()); | |
146 SkScalar dstTop = std::min(0, -cropRect.y()); | |
147 if (cropRect.x() < 0) | |
148 dstLeft = -cropRect.x(); | |
149 if (cropRect.y() < 0) | |
150 dstTop = -cropRect.y(); | |
151 surface->getCanvas()->drawImage(image.get(), dstLeft, dstTop); | |
152 return adoptRef(surface->newImageSnapshot()); | |
153 } | |
154 | |
155 unsigned long ImageBitmap::width() const | 155 unsigned long ImageBitmap::width() const |
156 { | 156 { |
157 if (!m_image) | 157 if (!m_image) |
158 return 0; | 158 return 0; |
159 ASSERT(m_image->width() > 0); | 159 ASSERT(m_image->width() > 0); |
160 return m_image->width(); | 160 return m_image->width(); |
161 } | 161 } |
162 | 162 |
163 unsigned long ImageBitmap::height() const | 163 unsigned long ImageBitmap::height() const |
164 { | 164 { |
(...skipping 16 matching lines...) Expand all Loading... |
181 } | 181 } |
182 | 182 |
183 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status
, AccelerationHint) const | 183 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status
, AccelerationHint) const |
184 { | 184 { |
185 *status = NormalSourceImageStatus; | 185 *status = NormalSourceImageStatus; |
186 return m_image ? StaticBitmapImage::create(m_image) : nullptr; | 186 return m_image ? StaticBitmapImage::create(m_image) : nullptr; |
187 } | 187 } |
188 | 188 |
189 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const | 189 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const |
190 { | 190 { |
191 | |
192 } | 191 } |
193 | 192 |
194 FloatSize ImageBitmap::elementSize() const | 193 FloatSize ImageBitmap::elementSize() const |
195 { | 194 { |
196 return FloatSize(width(), height()); | 195 return FloatSize(width(), height()); |
197 } | 196 } |
198 | 197 |
199 DEFINE_TRACE(ImageBitmap) | 198 DEFINE_TRACE(ImageBitmap) |
200 { | 199 { |
201 ImageLoaderClient::trace(visitor); | 200 ImageLoaderClient::trace(visitor); |
202 } | 201 } |
203 | 202 |
204 } // namespace blink | 203 } // namespace blink |
OLD | NEW |