Chromium Code Reviews| 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/page/ImageBitmap.h" | 6 #include "core/page/ImageBitmap.h" |
| 7 | 7 |
| 8 #include "core/fileapi/Blob.h" | |
| 8 #include "core/html/HTMLCanvasElement.h" | 9 #include "core/html/HTMLCanvasElement.h" |
| 9 #include "core/html/HTMLImageElement.h" | 10 #include "core/html/HTMLImageElement.h" |
| 10 #include "core/html/HTMLVideoElement.h" | 11 #include "core/html/HTMLVideoElement.h" |
| 11 #include "core/html/ImageData.h" | 12 #include "core/html/ImageData.h" |
| 12 #include "core/page/ImageBitmapCallback.h" | 13 #include "core/loader/cache/FetchRequest.h" |
| 14 #include "core/loader/cache/ResourceFetcher.h" | |
| 13 #include "core/platform/graphics/GraphicsContext.h" | 15 #include "core/platform/graphics/GraphicsContext.h" |
| 14 #include "wtf/RefPtr.h" | 16 #include "wtf/RefPtr.h" |
| 15 | 17 |
| 16 using namespace std; | 18 using namespace std; |
| 17 | 19 |
| 18 namespace WebCore { | 20 namespace WebCore { |
| 19 | 21 |
| 20 static inline IntRect normalizeRect(const IntRect rect) | 22 static inline IntRect normalizeRect(const IntRect rect) |
| 21 { | 23 { |
| 22 return IntRect(min(rect.x(), rect.maxX()), | 24 return IntRect(min(rect.x(), rect.maxX()), |
| 23 min(rect.y(), rect.maxY()), | 25 min(rect.y(), rect.maxY()), |
| 24 max(rect.width(), -rect.width()), | 26 max(rect.width(), -rect.width()), |
| 25 max(rect.height(), -rect.height())); | 27 max(rect.height(), -rect.height())); |
| 26 } | 28 } |
| 27 | 29 |
| 28 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) | 30 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) |
| 29 { | 31 { |
| 30 SkBitmap cropped; | 32 SkBitmap cropped; |
| 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); | 33 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); |
| 32 return BitmapImage::create(NativeImageSkia::create(cropped)); | 34 return BitmapImage::create(NativeImageSkia::create(cropped)); |
| 33 } | 35 } |
| 34 | 36 |
| 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) | 37 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) |
| 36 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 38 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 37 , m_size(cropRect.size()) | 39 , m_cropRect(cropRect) |
| 40 , m_window(0) | |
| 38 { | 41 { |
| 39 Image* bitmapImage = image->cachedImage()->image(); | 42 Image* bitmapImage = image->cachedImage()->image(); |
| 40 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 43 m_bitmap = cropImage(bitmapImage, cropRect).get(); |
| 41 | 44 |
| 42 ScriptWrappable::init(this); | 45 ScriptWrappable::init(this); |
| 43 } | 46 } |
| 44 | 47 |
| 45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) | 48 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) |
| 46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 49 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 47 , m_size(cropRect.size()) | 50 , m_cropRect(cropRect) |
| 51 , m_window(0) | |
| 48 { | 52 { |
| 49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); | 53 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); |
| 50 IntRect srcRect = intersection(cropRect, videoRect); | 54 IntRect srcRect = intersection(cropRect, videoRect); |
| 51 IntRect dstRect(IntPoint(), srcRect.size()); | 55 IntRect dstRect(IntPoint(), srcRect.size()); |
| 52 | 56 |
| 53 m_buffer = ImageBuffer::create(videoRect.size()); | 57 m_buffer = ImageBuffer::create(videoRect.size()); |
| 54 GraphicsContext* c = m_buffer->context(); | 58 GraphicsContext* c = m_buffer->context(); |
| 55 c->clip(dstRect); | 59 c->clip(dstRect); |
| 56 c->translate(-srcRect.x(), -srcRect.y()); | 60 c->translate(-srcRect.x(), -srcRect.y()); |
| 57 video->paintCurrentFrameInContext(c, videoRect); | 61 video->paintCurrentFrameInContext(c, videoRect); |
| 58 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 62 m_bitmap = m_buffer->copyImage(DontCopyBackingStore).get(); |
| 59 | 63 |
| 60 ScriptWrappable::init(this); | 64 ScriptWrappable::init(this); |
| 61 } | 65 } |
| 62 | 66 |
| 63 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) | 67 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) |
| 64 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 68 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 65 , m_size(cropRect.size()) | 69 , m_cropRect(cropRect) |
| 70 , m_window(0) | |
| 66 { | 71 { |
| 67 IntSize canvasSize = canvas->size(); | 72 IntSize canvasSize = canvas->size(); |
| 68 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); | 73 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); |
| 69 IntRect dstRect(IntPoint(), srcRect.size()); | 74 IntRect dstRect(IntPoint(), srcRect.size()); |
| 70 | 75 |
| 71 m_buffer = ImageBuffer::create(canvasSize); | 76 m_buffer = ImageBuffer::create(canvasSize); |
| 72 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); | 77 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); |
| 73 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 78 m_bitmap = m_buffer->copyImage(DontCopyBackingStore).get(); |
| 74 | 79 |
| 75 ScriptWrappable::init(this); | 80 ScriptWrappable::init(this); |
| 76 } | 81 } |
| 77 | 82 |
| 83 ImageBitmap::ImageBitmap(DOMWindow* window, PassRefPtr<ImageBitmapCallback> call back, Blob* blob, IntRect cropRect) | |
| 84 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | |
| 85 , m_cropRect(cropRect) | |
| 86 , m_bitmap(0) | |
| 87 , m_window(window) | |
| 88 , m_callback(callback) | |
| 89 { | |
| 90 m_loader = adoptPtr(new FileReaderLoader(FileReaderLoader::ReadAsDataURL, th is)); | |
|
Justin Novosad
2013/07/26 18:15:58
This loading functionality should not live in Imag
| |
| 91 m_loader->setDataType(blob->type()); | |
| 92 m_loader->start(m_window->document()->scriptExecutionContext(), *blob); | |
| 93 ScriptWrappable::init(this); | |
| 94 } | |
| 95 | |
| 78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) | 96 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) |
| 79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 97 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 80 , m_size(cropRect.size()) | 98 , m_cropRect(cropRect) |
| 99 , m_window(0) | |
| 81 { | 100 { |
| 82 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 101 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 83 | 102 |
| 84 m_buffer = ImageBuffer::create(data->size()); | 103 m_buffer = ImageBuffer::create(data->size()); |
| 85 if (srcRect.width() > 0 && srcRect.height() > 0) | 104 if (srcRect.width() > 0 && srcRect.height() > 0) |
| 86 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); | 105 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
| 87 | 106 |
| 88 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 107 m_bitmap = m_buffer->copyImage(DontCopyBackingStore).get(); |
| 89 | 108 |
| 90 ScriptWrappable::init(this); | 109 ScriptWrappable::init(this); |
| 91 } | 110 } |
| 92 | 111 |
| 93 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) | 112 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) |
| 94 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y())) | 113 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y())) |
| 95 , m_size(cropRect.size()) | 114 , m_cropRect(cropRect) |
| 115 , m_window(0) | |
| 96 { | 116 { |
| 97 Image* bitmapImage = bitmap->bitmapImage(); | 117 Image* bitmapImage = bitmap->bitmapImage(); |
| 98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); | 118 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); |
| 99 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 119 m_bitmap = cropImage(bitmapImage, cropRect).get(); |
| 100 | 120 |
| 101 ScriptWrappable::init(this); | 121 ScriptWrappable::init(this); |
| 102 } | 122 } |
| 103 | 123 |
| 124 ImageBitmap::~ImageBitmap() | |
| 125 { | |
| 126 // If the ImageBitmap was destroyed before the bitmap has resolved from a bl ob. | |
| 127 if (m_window && !m_bitmap) | |
| 128 m_window->imageBitmapFinishedLoading(this); | |
| 129 } | |
| 130 | |
| 104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) | 131 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) |
| 105 { | 132 { |
| 106 IntRect normalizedCropRect = normalizeRect(cropRect); | 133 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); | 134 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); |
| 108 return imageBitmap.release(); | 135 return imageBitmap.release(); |
| 109 } | 136 } |
| 110 | 137 |
| 111 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) | 138 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) |
| 112 { | 139 { |
| 113 IntRect normalizedCropRect = normalizeRect(cropRect); | 140 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 114 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); | 141 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); |
| 115 return imageBitmap.release(); | 142 return imageBitmap.release(); |
| 116 } | 143 } |
| 117 | 144 |
| 118 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) | 145 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) |
| 119 { | 146 { |
| 120 IntRect normalizedCropRect = normalizeRect(cropRect); | 147 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 121 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); | 148 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); |
| 122 return imageBitmap.release(); | 149 return imageBitmap.release(); |
| 123 } | 150 } |
| 124 | 151 |
| 152 PassRefPtr<ImageBitmap> ImageBitmap::create(DOMWindow* window, PassRefPtr<ImageB itmapCallback> callback, Blob* blob, IntRect cropRect) | |
| 153 { | |
| 154 IntRect normalizedCropRect = normalizeRect(cropRect); | |
| 155 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(window, callback, b lob, normalizedCropRect))); | |
| 156 return imageBitmap.release(); | |
| 157 } | |
| 158 | |
| 125 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) | 159 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) |
| 126 { | 160 { |
| 127 IntRect normalizedCropRect = normalizeRect(cropRect); | 161 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 128 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); | 162 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); |
| 129 return imageBitmap.release(); | 163 return imageBitmap.release(); |
| 130 } | 164 } |
| 131 | 165 |
| 132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) | 166 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) |
| 133 { | 167 { |
| 134 IntRect normalizedCropRect = normalizeRect(cropRect); | 168 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); | 169 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); |
| 136 return imageBitmap.release(); | 170 return imageBitmap.release(); |
| 137 } | 171 } |
| 138 | 172 |
| 173 // FileReaderLoaderClient | |
| 174 void ImageBitmap::didFinishLoading() | |
| 175 { | |
| 176 FetchRequest request(ResourceRequest(m_window->document()->completeURL(m_loa der->stringResult())), ""); | |
| 177 m_pendingCachedImage = m_window->document()->fetcher()->requestImage(request ); | |
| 178 m_pendingCachedImage->addClient(this); | |
| 139 } | 179 } |
| 180 | |
| 181 void ImageBitmap::didFail(FileError::ErrorCode errorCode) | |
| 182 { | |
| 183 // Read the Blob object's data. If an error occurs during reading of the obj ect, then reject the | |
| 184 // Promise's associated resolver, with null as the value, and abort these st eps. | |
| 185 | |
| 186 // If the image data is not in a supported file format (e.g. it's not actual ly an image at all), | |
| 187 // or if the image data is corrupted in some fatal way such that the image d imensions cannot be obtained, | |
| 188 // then reject the Promise's associated resolver, with null as the value, an d abort these steps. | |
| 189 } | |
| 190 | |
| 191 // CachedImageClient | |
| 192 void ImageBitmap::notifyFinished(CachedResource* resource) | |
| 193 { | |
| 194 Image* image = static_cast<CachedImage*>(resource)->image(); | |
| 195 if (!m_cropRect.width() && !m_cropRect.height()) { | |
| 196 // No crop rect was specified | |
| 197 m_cropRect = IntRect(IntPoint(), image->size()); | |
| 198 m_bitmap = image; | |
| 199 } else { | |
| 200 m_bitmap = cropImage(image, m_cropRect); | |
| 201 } | |
| 202 m_window->scriptExecutionContext()->postTask(ImageBitmapCallback::CallbackTa sk::create(this, m_callback)); | |
| 203 m_window->imageBitmapFinishedLoading(this); | |
| 204 } | |
| 205 | |
| 206 } | |
| OLD | NEW |