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" | |
17 #include "third_party/skia/include/core/SkSurface.h" | |
16 #include "wtf/RefPtr.h" | 18 #include "wtf/RefPtr.h" |
17 | 19 |
18 namespace blink { | 20 namespace blink { |
19 | 21 |
20 static inline IntRect normalizeRect(const IntRect& rect) | 22 static inline IntRect normalizeRect(const IntRect& rect) |
21 { | 23 { |
22 return IntRect(std::min(rect.x(), rect.maxX()), | 24 return IntRect(std::min(rect.x(), rect.maxX()), |
23 std::min(rect.y(), rect.maxY()), | 25 std::min(rect.y(), rect.maxY()), |
24 std::max(rect.width(), -rect.width()), | 26 std::max(rect.width(), -rect.width()), |
25 std::max(rect.height(), -rect.height())); | 27 std::max(rect.height(), -rect.height())); |
26 } | 28 } |
27 | 29 |
28 static inline PassRefPtr<Image> cropImage(PassRefPtr<Image> image, const IntRect & cropRect) | 30 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
29 { | 31 { |
30 ASSERT(image); | 32 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c ropRect); |
31 | |
32 const SkIRect srcRect = intersection(image->rect(), cropRect); | |
33 if (srcRect.isEmpty()) | |
34 return nullptr; | |
35 | |
36 RefPtr<SkImage> skImage = image->imageForCurrentFrame(); | |
37 if (!skImage) | |
38 return nullptr; | |
39 | |
40 return StaticBitmapImage::create(adoptRef(skImage->newSubset(srcRect))); | |
41 } | |
42 | |
43 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | |
44 : m_imageElement(image) | |
45 , m_bitmap(nullptr) | |
46 , m_cropRect(cropRect) | |
47 { | |
48 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image ->height())); | |
49 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); | |
50 m_bitmapOffset = srcRect.location(); | |
51 | |
52 if (!srcRect.width() || !srcRect.height()) | |
53 m_imageElement = nullptr; | |
54 else | |
55 m_imageElement->addClient(this); | |
56 } | 33 } |
57 | 34 |
58 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 35 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
59 : m_imageElement(nullptr) | |
60 , m_cropRect(cropRect) | |
61 , m_bitmapOffset(IntPoint()) | |
62 { | 36 { |
63 IntSize playerSize; | 37 IntSize playerSize; |
64 | |
65 if (video->webMediaPlayer()) | 38 if (video->webMediaPlayer()) |
66 playerSize = video->webMediaPlayer()->naturalSize(); | 39 playerSize = video->webMediaPlayer()->naturalSize(); |
67 | 40 |
68 IntRect videoRect = IntRect(IntPoint(), playerSize); | 41 IntRect videoRect = IntRect(IntPoint(), playerSize); |
69 IntRect srcRect = intersection(cropRect, videoRect); | 42 IntRect srcRect = intersection(cropRect, videoRect); |
70 IntRect dstRect(IntPoint(), srcRect.size()); | 43 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); |
71 | |
72 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size()); | |
73 if (!buffer) | 44 if (!buffer) |
74 return; | 45 return; |
75 | 46 |
76 buffer->canvas()->clipRect(dstRect); | 47 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); |
77 buffer->canvas()->translate(-srcRect.x(), -srcRect.y()); | 48 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); |
78 | 49 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); |
79 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr); | |
80 m_bitmap = buffer->newImageSnapshot(); | |
81 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); | |
82 } | 50 } |
83 | 51 |
84 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 52 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
85 : m_imageElement(nullptr) | |
86 , m_cropRect(cropRect) | |
87 , m_bitmapOffset(IntPoint()) | |
88 { | 53 { |
89 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()) ); | |
90 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); | |
91 ASSERT(canvas->isPaintable()); | 54 ASSERT(canvas->isPaintable()); |
92 m_bitmap = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration), cr opRect); | 55 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima geForCurrentFrame(), cropRect); |
93 } | 56 } |
94 | 57 |
95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 58 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
96 : m_imageElement(nullptr) | |
97 , m_cropRect(cropRect) | |
98 , m_bitmapOffset(IntPoint()) | |
99 { | 59 { |
100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 60 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
101 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels); | 61 |
62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); | |
102 if (!buffer) | 63 if (!buffer) |
103 return; | 64 return; |
104 | 65 |
105 if (srcRect.width() > 0 && srcRect.height() > 0) | 66 if (srcRect.isEmpty()) { |
106 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), s rcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y()))); | 67 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); |
68 return; | |
69 } | |
107 | 70 |
108 m_bitmap = buffer->newImageSnapshot(); | 71 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y())); |
109 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); | 72 if (cropRect.x() < 0) |
73 dstPoint.setX(-cropRect.x()); | |
74 if (cropRect.y() < 0) | |
75 dstPoint.setY(-cropRect.y()); | |
76 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); | |
77 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration); | |
110 } | 78 } |
111 | 79 |
112 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 80 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
113 : m_imageElement(bitmap->imageElement()) | |
114 , m_bitmap(nullptr) | |
115 , m_cropRect(cropRect) | |
116 , m_bitmapOffset(IntPoint()) | |
117 { | 81 { |
118 IntRect oldBitmapRect = bitmap->bitmapRect(); | 82 m_image = cropImage(bitmap->skImage(), cropRect); |
119 IntRect srcRect = intersection(cropRect, oldBitmapRect); | |
120 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x() ), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); | |
121 | |
122 if (m_imageElement) { | |
123 m_imageElement->addClient(this); | |
124 m_bitmapOffset = srcRect.location(); | |
125 } else if (bitmap->bitmapImage()) { | |
126 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR ect.y() - oldBitmapRect.y()), cropRect.size()); | |
127 m_bitmap = cropImage(bitmap->bitmapImage(), adjustedCropRect); | |
128 } | |
129 } | 83 } |
130 | 84 |
131 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 85 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
132 : m_imageElement(nullptr) | |
133 , m_cropRect(cropRect) | |
134 { | 86 { |
135 IntRect srcRect = intersection(cropRect, image->rect()); | 87 m_image = cropImage(image->imageForCurrentFrame(), cropRect); |
136 m_bitmap = cropImage(image, cropRect); | |
137 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); | |
138 } | 88 } |
139 | 89 |
140 ImageBitmap::~ImageBitmap() | 90 ImageBitmap::~ImageBitmap() |
141 { | 91 { |
142 #if !ENABLE(OILPAN) | |
143 if (m_imageElement) | |
144 m_imageElement->removeClient(this); | |
145 #endif | |
146 } | 92 } |
147 | 93 |
148 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect) | 94 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect) |
149 { | 95 { |
150 IntRect normalizedCropRect = normalizeRect(cropRect); | 96 IntRect normalizedCropRect = normalizeRect(cropRect); |
151 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 97 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
152 } | 98 } |
153 | 99 |
154 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect) | 100 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect) |
155 { | 101 { |
(...skipping 18 matching lines...) Expand all Loading... | |
174 IntRect normalizedCropRect = normalizeRect(cropRect); | 120 IntRect normalizedCropRect = normalizeRect(cropRect); |
175 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); | 121 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); |
176 } | 122 } |
177 | 123 |
178 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect) | 124 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect) |
179 { | 125 { |
180 IntRect normalizedCropRect = normalizeRect(cropRect); | 126 IntRect normalizedCropRect = normalizeRect(cropRect); |
181 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); | 127 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); |
182 } | 128 } |
183 | 129 |
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 SkImageInfo info = SkImageInfo::MakeN32(cropRect.width(), cropRect.height(), kPremul_SkAlphaType); | |
141 SkSurface* surface = SkSurface::NewRaster(info); | |
Justin Novosad
2015/11/04 15:20:45
Nit: you can use the shorthand SkSurface::NewRaste
| |
142 | |
143 if (srcRect.isEmpty()) | |
144 return adoptRef(surface->newImageSnapshot()); | |
145 | |
146 SkScalar dstLeft = std::min(0, -cropRect.x()); | |
147 SkScalar dstTop = std::min(0, -cropRect.y()); | |
148 if (cropRect.x() < 0) | |
149 dstLeft = -cropRect.x(); | |
150 if (cropRect.y() < 0) | |
151 dstTop = -cropRect.y(); | |
152 surface->getCanvas()->drawImage(image.get(), dstLeft, dstTop); | |
153 return adoptRef(surface->newImageSnapshot()); | |
154 } | |
155 | |
184 void ImageBitmap::notifyImageSourceChanged() | 156 void ImageBitmap::notifyImageSourceChanged() |
185 { | 157 { |
186 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | |
187 m_bitmapOffset = IntPoint(); | |
188 m_imageElement = nullptr; | |
189 } | |
190 | |
191 PassRefPtr<Image> ImageBitmap::bitmapImage() const | |
192 { | |
193 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect .height()) && (!m_imageElement || !m_bitmap)); | |
194 if (m_imageElement) | |
195 return m_imageElement->cachedImage()->image(); | |
196 return m_bitmap; | |
197 } | 158 } |
198 | 159 |
199 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const | 160 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const |
200 { | 161 { |
201 *status = NormalSourceImageStatus; | 162 *status = NormalSourceImageStatus; |
202 return bitmapImage(); | 163 return m_image ? StaticBitmapImage::create(m_image) : nullptr; |
203 } | 164 } |
204 | 165 |
205 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const | 166 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const |
206 { | 167 { |
207 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect); | 168 |
208 FloatRect newSrcRect = intersectRect; | |
209 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location()); | |
210 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), m_bitmapRect.size()); | |
211 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(), | |
212 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitma pRect.height()); | |
213 newDstRect.moveBy(dstRect->location()); | |
214 *srcRect = newSrcRect; | |
215 *dstRect = newDstRect; | |
216 } | 169 } |
217 | 170 |
218 FloatSize ImageBitmap::elementSize() const | 171 FloatSize ImageBitmap::elementSize() const |
219 { | 172 { |
220 return FloatSize(width(), height()); | 173 return FloatSize(width(), height()); |
221 } | 174 } |
222 | 175 |
223 DEFINE_TRACE(ImageBitmap) | 176 DEFINE_TRACE(ImageBitmap) |
224 { | 177 { |
225 visitor->trace(m_imageElement); | |
226 ImageLoaderClient::trace(visitor); | 178 ImageLoaderClient::trace(visitor); |
227 } | 179 } |
228 | 180 |
229 } // namespace blink | 181 } // namespace blink |
OLD | NEW |