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" | 12 #include "core/platform/graphics/BitmapImage.h" |
| 13 #include "core/platform/graphics/GraphicsContext.h" | 13 #include "core/platform/graphics/GraphicsContext.h" |
| 14 #include "wtf/RefPtr.h" | 14 #include "wtf/RefPtr.h" |
| 15 | 15 |
| 16 using namespace std; | 16 using namespace std; |
| 17 | 17 |
| 18 namespace WebCore { | 18 namespace WebCore { |
| 19 | 19 |
| 20 static inline IntRect normalizeRect(const IntRect rect) | 20 static inline IntRect normalizeRect(const IntRect& rect) |
| 21 { | 21 { |
| 22 return IntRect(min(rect.x(), rect.maxX()), | 22 return IntRect(min(rect.x(), rect.maxX()), |
| 23 min(rect.y(), rect.maxY()), | 23 min(rect.y(), rect.maxY()), |
| 24 max(rect.width(), -rect.width()), | 24 max(rect.width(), -rect.width()), |
| 25 max(rect.height(), -rect.height())); | 25 max(rect.height(), -rect.height())); |
| 26 } | 26 } |
| 27 | 27 |
| 28 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) | 28 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) |
| 29 { | 29 { |
| 30 SkBitmap cropped; | 30 SkBitmap cropped; |
| 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); | 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); |
| 32 return BitmapImage::create(NativeImageSkia::create(cropped)); | 32 return BitmapImage::create(NativeImageSkia::create(cropped)); |
| 33 } | 33 } |
| 34 | 34 |
| 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) | 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 36 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 36 : m_cropRect(cropRect) |
| 37 , m_size(cropRect.size()) | 37 , m_imageElement(image) |
| 38 { | 38 { |
| 39 Image* bitmapImage = image->cachedImage()->image(); | 39 m_imageElement->addClient(this); |
| 40 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 40 m_imageElement->cachedImage()->setCachePriority(CachedResource::CachePriorit yHigh); |
|
Nate Chapin
2013/07/26 17:05:12
Is there any way that we could get setting this va
Justin Novosad
2013/07/29 14:58:00
On top of that, the current implementation lacks a
| |
| 41 | |
| 42 IntSize bitmapSize = intersection(cropRect, IntRect(0, 0, image->width(), im age->height())).size(); | |
| 43 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), bitmapSize); | |
| 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, const IntRect& cropRect) |
| 46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 49 : m_cropRect(cropRect) |
| 47 , m_size(cropRect.size()) | 50 , m_imageElement(0) |
| 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_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.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_cropRect(cropRect) |
| 65 , m_size(cropRect.size()) | 69 , m_imageElement(0) |
| 66 { | 70 { |
| 67 IntSize canvasSize = canvas->size(); | 71 IntSize canvasSize = canvas->size(); |
| 68 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); | 72 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); |
| 69 IntRect dstRect(IntPoint(), srcRect.size()); | 73 IntRect dstRect(IntPoint(), srcRect.size()); |
| 70 | 74 |
| 71 m_buffer = ImageBuffer::create(canvasSize); | 75 m_buffer = ImageBuffer::create(canvasSize); |
| 72 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); | 76 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); |
| 73 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 77 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 78 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.size()); | |
| 74 | 79 |
| 75 ScriptWrappable::init(this); | 80 ScriptWrappable::init(this); |
| 76 } | 81 } |
| 77 | 82 |
| 78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) | 83 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 84 : m_cropRect(cropRect) |
| 80 , m_size(cropRect.size()) | 85 , m_imageElement(0) |
| 81 { | 86 { |
| 82 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 87 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 83 | 88 |
| 84 m_buffer = ImageBuffer::create(data->size()); | 89 m_buffer = ImageBuffer::create(data->size()); |
| 85 if (srcRect.width() > 0 && srcRect.height() > 0) | 90 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()))); | 91 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
| 87 | 92 |
| 88 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); | 93 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 94 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.size()); | |
| 89 | 95 |
| 90 ScriptWrappable::init(this); | 96 ScriptWrappable::init(this); |
| 91 } | 97 } |
| 92 | 98 |
| 93 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) | 99 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())) | 100 : m_cropRect(cropRect) |
| 95 , m_size(cropRect.size()) | 101 , m_imageElement(bitmap->imageElement()) |
| 96 { | 102 { |
| 97 Image* bitmapImage = bitmap->bitmapImage(); | 103 IntRect oldBitmapRect = bitmap->bitmapRect(); |
| 98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); | 104 IntSize bitmapSize = intersection(cropRect, oldBitmapRect).size(); |
| 99 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 105 IntPoint bitmapOffset(max(0, oldBitmapRect.x() - cropRect.x()), max(0, oldBi tmapRect.y() - cropRect.y())); |
| 100 | 106 m_bitmapRect = IntRect(bitmapOffset, bitmapSize); |
| 107 if (m_imageElement) { | |
| 108 m_imageElement->addClient(this); | |
| 109 m_bitmap = 0; | |
| 110 } else { | |
| 111 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR ect.y() - oldBitmapRect.y()), cropRect.size()); | |
| 112 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); | |
| 113 } | |
| 101 ScriptWrappable::init(this); | 114 ScriptWrappable::init(this); |
| 102 } | 115 } |
| 103 | 116 |
| 104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) | 117 ImageBitmap::~ImageBitmap() |
| 118 { | |
| 119 if (m_imageElement) | |
| 120 m_imageElement->removeClient(this); | |
| 121 } | |
| 122 | |
| 123 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe ct& cropRect) | |
| 105 { | 124 { |
| 106 IntRect normalizedCropRect = normalizeRect(cropRect); | 125 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); | 126 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); |
| 108 return imageBitmap.release(); | 127 return imageBitmap.release(); |
| 109 } | 128 } |
| 110 | 129 |
| 111 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) | 130 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRe ct& cropRect) |
| 112 { | 131 { |
| 113 IntRect normalizedCropRect = normalizeRect(cropRect); | 132 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 114 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); | 133 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); |
| 115 return imageBitmap.release(); | 134 return imageBitmap.release(); |
| 116 } | 135 } |
| 117 | 136 |
| 118 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) | 137 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const Int Rect& cropRect) |
| 119 { | 138 { |
| 120 IntRect normalizedCropRect = normalizeRect(cropRect); | 139 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 121 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); | 140 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); |
| 122 return imageBitmap.release(); | 141 return imageBitmap.release(); |
| 123 } | 142 } |
| 124 | 143 |
| 125 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) | 144 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& crop Rect) |
| 126 { | 145 { |
| 127 IntRect normalizedCropRect = normalizeRect(cropRect); | 146 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 128 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); | 147 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); |
| 129 return imageBitmap.release(); | 148 return imageBitmap.release(); |
| 130 } | 149 } |
| 131 | 150 |
| 132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) | 151 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect) |
| 133 { | 152 { |
| 134 IntRect normalizedCropRect = normalizeRect(cropRect); | 153 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); | 154 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); |
| 136 return imageBitmap.release(); | 155 return imageBitmap.release(); |
| 137 } | 156 } |
| 138 | 157 |
| 158 void ImageBitmap::notifyImageSourceChanged() | |
| 159 { | |
| 160 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | |
| 161 m_imageElement = 0; | |
| 139 } | 162 } |
| 163 | |
| 164 PassRefPtr<Image> ImageBitmap::bitmapImage() const | |
| 165 { | |
| 166 ASSERT((m_imageElement || m_bitmap) && (!m_imageElement || !m_bitmap)); | |
| 167 if (m_imageElement) | |
| 168 return cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | |
| 169 return m_bitmap; | |
| 170 } | |
| 171 | |
| 172 } | |
| OLD | NEW |