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/html/HTMLCanvasElement.h" | 8 #include "core/html/HTMLCanvasElement.h" |
| 9 #include "core/html/HTMLImageElement.h" | 9 #include "core/html/HTMLImageElement.h" |
| 10 #include "core/html/HTMLVideoElement.h" | 10 #include "core/html/HTMLVideoElement.h" |
| 11 #include "core/html/ImageData.h" | 11 #include "core/html/ImageData.h" |
| 12 #include "core/page/ImageBitmapCallback.h" | |
| 13 #include "core/platform/graphics/GraphicsContext.h" | 12 #include "core/platform/graphics/GraphicsContext.h" |
| 14 #include "wtf/RefPtr.h" | 13 #include "wtf/RefPtr.h" |
| 15 | 14 |
| 16 using namespace std; | 15 using namespace std; |
| 17 | 16 |
| 18 namespace WebCore { | 17 namespace WebCore { |
| 19 | 18 |
| 20 static inline IntRect normalizeRect(const IntRect rect) | 19 static inline IntRect normalizeRect(const IntRect& rect) |
| 21 { | 20 { |
| 22 return IntRect(min(rect.x(), rect.maxX()), | 21 return IntRect(min(rect.x(), rect.maxX()), |
| 23 min(rect.y(), rect.maxY()), | 22 min(rect.y(), rect.maxY()), |
| 24 max(rect.width(), -rect.width()), | 23 max(rect.width(), -rect.width()), |
| 25 max(rect.height(), -rect.height())); | 24 max(rect.height(), -rect.height())); |
| 26 } | 25 } |
| 27 | 26 |
| 28 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) | 27 static inline PassRefPtr<BitmapImage> cropImage(Image* image, const IntRect& cro pRect) |
| 29 { | 28 { |
| 30 SkBitmap cropped; | 29 SkBitmap cropped; |
| 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); | 30 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); |
| 32 return BitmapImage::create(NativeImageSkia::create(cropped)); | 31 return BitmapImage::create(NativeImageSkia::create(cropped)); |
| 33 } | 32 } |
| 34 | 33 |
| 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) | 34 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 36 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 35 : m_imageElement(image) |
| 37 , m_size(cropRect.size()) | 36 , m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 37 , m_cropRect(cropRect) | |
| 38 { | 38 { |
| 39 Image* bitmapImage = image->cachedImage()->image(); | 39 CachedImage* cachedImage = m_imageElement->cachedImage(); |
| 40 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 40 cachedImage->resourceRequest().setPriority(ResourceLoadPriorityVeryHigh); |
| 41 cachedImage->didChangePriority(ResourceLoadPriorityVeryHigh); | |
| 42 m_bitmapSize = intersection(cropRect, IntRect(IntPoint(), cachedImage->image ()->size())).size(); | |
| 41 | 43 |
| 42 ScriptWrappable::init(this); | 44 ScriptWrappable::init(this); |
| 43 } | 45 } |
| 44 | 46 |
| 45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) | 47 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
| 46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 48 : m_imageElement(0) |
| 47 , m_size(cropRect.size()) | 49 , m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 50 , m_cropRect(cropRect) | |
| 48 { | 51 { |
| 49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); | 52 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); |
| 50 IntRect srcRect = intersection(cropRect, videoRect); | 53 IntRect srcRect = intersection(cropRect, videoRect); |
| 51 IntRect dstRect(IntPoint(), srcRect.size()); | 54 IntRect dstRect(IntPoint(), srcRect.size()); |
| 52 | 55 |
| 53 m_buffer = ImageBuffer::create(videoRect.size()); | 56 m_buffer = ImageBuffer::create(videoRect.size()); |
| 54 GraphicsContext* c = m_buffer->context(); | 57 GraphicsContext* c = m_buffer->context(); |
| 55 c->clip(dstRect); | 58 c->clip(dstRect); |
| 56 c->translate(-srcRect.x(), -srcRect.y()); | 59 c->translate(-srcRect.x(), -srcRect.y()); |
| 57 video->paintCurrentFrameInContext(c, videoRect); | 60 video->paintCurrentFrameInContext(c, videoRect); |
| 58 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 61 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 62 m_bitmapSize = IntSize(m_bitmap->size()); | |
| 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, const IntRect& cropRect) |
| 64 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 68 : m_imageElement(0) |
| 65 , m_size(cropRect.size()) | 69 , m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 70 , m_cropRect(cropRect) | |
| 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); |
| 79 m_bitmapSize = IntSize(m_bitmap->size()); | |
| 74 | 80 |
| 75 ScriptWrappable::init(this); | 81 ScriptWrappable::init(this); |
| 76 } | 82 } |
| 77 | 83 |
| 78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) | 84 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 85 : m_imageElement(0) |
| 80 , m_size(cropRect.size()) | 86 , m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 87 , m_cropRect(cropRect) | |
| 81 { | 88 { |
| 82 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 89 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 83 | 90 |
| 84 m_buffer = ImageBuffer::create(data->size()); | 91 m_buffer = ImageBuffer::create(data->size()); |
| 85 if (srcRect.width() > 0 && srcRect.height() > 0) | 92 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()))); | 93 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
| 87 | 94 |
| 88 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 95 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 96 m_bitmapSize = IntSize(m_bitmap->size()); | |
| 89 | 97 |
| 90 ScriptWrappable::init(this); | 98 ScriptWrappable::init(this); |
| 91 } | 99 } |
| 92 | 100 |
| 93 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) | 101 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 94 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y())) | 102 : m_imageElement(0) |
| 95 , m_size(cropRect.size()) | 103 , m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y())) |
| 104 , m_cropRect(cropRect) | |
| 96 { | 105 { |
| 97 Image* bitmapImage = bitmap->bitmapImage(); | 106 RefPtr<Image> bitmapImage = bitmap->bitmapImage(); |
| 98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); | 107 IntRect adjustedCropRect(IntPoint(cropRect.x() - bitmap->bitmapOffset().x(), cropRect.y() - bitmap->bitmapOffset().y()), cropRect.size()); |
| 99 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 108 m_bitmap = cropImage(bitmapImage.get(), adjustedCropRect); |
|
Justin Novosad
2013/07/16 23:05:53
You can do better here. If the source ImageBitmap
| |
| 109 m_bitmapSize = IntSize(m_bitmap->size()); | |
| 100 | 110 |
| 101 ScriptWrappable::init(this); | 111 ScriptWrappable::init(this); |
| 102 } | 112 } |
| 103 | 113 |
| 104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) | 114 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe ct& cropRect) |
| 105 { | 115 { |
| 106 IntRect normalizedCropRect = normalizeRect(cropRect); | 116 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); | 117 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap( image, normalizedC ropRect))); |
| 108 return imageBitmap.release(); | 118 return imageBitmap.release(); |
| 109 } | 119 } |
| 110 | 120 |
| 111 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) | 121 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRe ct& cropRect) |
| 112 { | 122 { |
| 113 IntRect normalizedCropRect = normalizeRect(cropRect); | 123 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 114 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); | 124 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); |
| 115 return imageBitmap.release(); | 125 return imageBitmap.release(); |
| 116 } | 126 } |
| 117 | 127 |
| 118 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) | 128 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const Int Rect& cropRect) |
| 119 { | 129 { |
| 120 IntRect normalizedCropRect = normalizeRect(cropRect); | 130 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 121 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); | 131 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); |
| 122 return imageBitmap.release(); | 132 return imageBitmap.release(); |
| 123 } | 133 } |
| 124 | 134 |
| 125 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) | 135 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& crop Rect) |
| 126 { | 136 { |
| 127 IntRect normalizedCropRect = normalizeRect(cropRect); | 137 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 128 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); | 138 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); |
| 129 return imageBitmap.release(); | 139 return imageBitmap.release(); |
| 130 } | 140 } |
| 131 | 141 |
| 132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) | 142 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect) |
| 133 { | 143 { |
| 134 IntRect normalizedCropRect = normalizeRect(cropRect); | 144 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); | 145 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); |
| 136 return imageBitmap.release(); | 146 return imageBitmap.release(); |
| 137 } | 147 } |
| 138 | 148 |
| 149 PassRefPtr<Image> ImageBitmap::bitmapImage() | |
| 150 { | |
| 151 if (m_imageElement) | |
|
Justin Novosad
2013/07/16 23:05:53
This is a good place for an ASSERT to validate tha
| |
| 152 return cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | |
| 153 return m_bitmap; | |
| 139 } | 154 } |
| 155 | |
| 156 } | |
| OLD | NEW |