| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 static inline PassRefPtr<Image> cropImage(Image* image, const 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, const IntRect& cropRect) | 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 36 : m_cropRect(cropRect) | 36 : m_cropRect(cropRect) |
| 37 , m_imageElement(image) | 37 , m_imageElement(image) |
| 38 , m_bitmap(0) |
| 39 , m_derivedFromCanvas(false) |
| 38 { | 40 { |
| 39 m_imageElement->addClient(this); | 41 m_imageElement->addClient(this); |
| 40 | 42 |
| 41 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); | 43 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); |
| 42 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 44 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 43 m_bitmapOffset = srcRect.location(); | 45 m_bitmapOffset = srcRect.location(); |
| 44 | 46 |
| 45 ScriptWrappable::init(this); | 47 ScriptWrappable::init(this); |
| 46 } | 48 } |
| 47 | 49 |
| 48 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) | 50 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) |
| 49 : m_cropRect(cropRect) | 51 : m_cropRect(cropRect) |
| 50 , m_imageElement(0) | 52 , m_imageElement(0) |
| 51 , m_bitmapOffset(IntPoint()) | 53 , m_bitmapOffset(IntPoint()) |
| 54 , m_derivedFromCanvas(false) |
| 52 { | 55 { |
| 53 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); | 56 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); |
| 54 IntRect srcRect = intersection(cropRect, videoRect); | 57 IntRect srcRect = intersection(cropRect, videoRect); |
| 55 IntRect dstRect(IntPoint(), srcRect.size()); | 58 IntRect dstRect(IntPoint(), srcRect.size()); |
| 56 | 59 |
| 57 m_buffer = ImageBuffer::create(videoRect.size()); | 60 m_buffer = ImageBuffer::create(videoRect.size()); |
| 58 GraphicsContext* c = m_buffer->context(); | 61 GraphicsContext* c = m_buffer->context(); |
| 59 c->clip(dstRect); | 62 c->clip(dstRect); |
| 60 c->translate(-srcRect.x(), -srcRect.y()); | 63 c->translate(-srcRect.x(), -srcRect.y()); |
| 61 video->paintCurrentFrameInContext(c, videoRect); | 64 video->paintCurrentFrameInContext(c, videoRect); |
| 62 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); | 65 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 63 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 66 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 64 | 67 |
| 65 ScriptWrappable::init(this); | 68 ScriptWrappable::init(this); |
| 66 } | 69 } |
| 67 | 70 |
| 68 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 71 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
| 69 : m_cropRect(cropRect) | 72 : m_cropRect(cropRect) |
| 70 , m_imageElement(0) | 73 , m_imageElement(0) |
| 74 , m_bitmap(canvas->buffer()->imageSnapshot()) |
| 71 , m_bitmapOffset(IntPoint()) | 75 , m_bitmapOffset(IntPoint()) |
| 76 , m_derivedFromCanvas(true) |
| 72 { | 77 { |
| 73 IntSize canvasSize = canvas->size(); | 78 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size())
); |
| 74 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); | |
| 75 IntRect dstRect(IntPoint(), srcRect.size()); | |
| 76 | |
| 77 m_buffer = ImageBuffer::create(canvasSize); | |
| 78 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); | |
| 79 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); | |
| 80 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 79 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 80 m_bitmapOffset = srcRect.location(); |
| 81 | 81 |
| 82 ScriptWrappable::init(this); | 82 ScriptWrappable::init(this); |
| 83 } | 83 } |
| 84 | 84 |
| 85 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 85 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 86 : m_cropRect(cropRect) | 86 : m_cropRect(cropRect) |
| 87 , m_imageElement(0) | 87 , m_imageElement(0) |
| 88 , m_bitmapOffset(IntPoint()) | 88 , m_bitmapOffset(IntPoint()) |
| 89 , m_derivedFromCanvas(false) |
| 89 { | 90 { |
| 90 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 91 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 91 | 92 |
| 92 m_buffer = ImageBuffer::create(data->size()); | 93 m_buffer = ImageBuffer::create(data->size()); |
| 93 if (srcRect.width() > 0 && srcRect.height() > 0) | 94 if (srcRect.width() > 0 && srcRect.height() > 0) |
| 94 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect
, IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); | 95 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect
, IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
| 95 | 96 |
| 96 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); | 97 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 97 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); | 98 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 98 | 99 |
| 99 ScriptWrappable::init(this); | 100 ScriptWrappable::init(this); |
| 100 } | 101 } |
| 101 | 102 |
| 102 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 103 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 103 : m_cropRect(cropRect) | 104 : m_cropRect(cropRect) |
| 104 , m_imageElement(bitmap->imageElement()) | 105 , m_imageElement(bitmap->imageElement()) |
| 105 , m_bitmapOffset(IntPoint()) | 106 , m_bitmapOffset(IntPoint()) |
| 107 , m_derivedFromCanvas(bitmap->derivedFromCanvas()) |
| 106 { | 108 { |
| 107 IntRect oldBitmapRect = bitmap->bitmapRect(); | 109 IntRect oldBitmapRect = bitmap->bitmapRect(); |
| 108 IntRect srcRect = intersection(cropRect, oldBitmapRect); | 110 IntRect srcRect = intersection(cropRect, oldBitmapRect); |
| 109 m_bitmapRect = IntRect(IntPoint(max(0, oldBitmapRect.x() - cropRect.x()), ma
x(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); | 111 m_bitmapRect = IntRect(IntPoint(max(0, oldBitmapRect.x() - cropRect.x()), ma
x(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); |
| 110 | 112 |
| 111 if (m_imageElement) { | 113 if (m_imageElement) { |
| 114 m_bitmap = 0; |
| 112 m_imageElement->addClient(this); | 115 m_imageElement->addClient(this); |
| 113 m_bitmap = 0; | 116 m_bitmapOffset = srcRect.location(); |
| 117 } else if (m_derivedFromCanvas) { |
| 118 m_bitmap = bitmap->bitmapImage(); |
| 114 m_bitmapOffset = srcRect.location(); | 119 m_bitmapOffset = srcRect.location(); |
| 115 } else { | 120 } else { |
| 116 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); | 121 IntRect adjustedCropRect(IntPoint(cropRect.x() - oldBitmapRect.x(), crop
Rect.y() - oldBitmapRect.y()), cropRect.size()); |
| 117 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); | 122 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); |
| 118 } | 123 } |
| 124 |
| 119 ScriptWrappable::init(this); | 125 ScriptWrappable::init(this); |
| 120 } | 126 } |
| 121 | 127 |
| 122 ImageBitmap::~ImageBitmap() | 128 ImageBitmap::~ImageBitmap() |
| 123 { | 129 { |
| 124 if (m_imageElement) | 130 if (m_imageElement) |
| 125 m_imageElement->removeClient(this); | 131 m_imageElement->removeClient(this); |
| 126 } | 132 } |
| 127 | 133 |
| 128 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe
ct& cropRect) | 134 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe
ct& cropRect) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 void ImageBitmap::notifyImageSourceChanged() | 169 void ImageBitmap::notifyImageSourceChanged() |
| 164 { | 170 { |
| 165 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | 171 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); |
| 166 m_bitmapOffset = IntPoint(); | 172 m_bitmapOffset = IntPoint(); |
| 167 m_imageElement = 0; | 173 m_imageElement = 0; |
| 168 } | 174 } |
| 169 | 175 |
| 170 PassRefPtr<Image> ImageBitmap::bitmapImage() const | 176 PassRefPtr<Image> ImageBitmap::bitmapImage() const |
| 171 { | 177 { |
| 172 ASSERT((m_imageElement || m_bitmap) && (!m_imageElement || !m_bitmap)); | 178 ASSERT((m_imageElement || m_bitmap) && (!m_imageElement || !m_bitmap)); |
| 179 ASSERT((m_imageElement || m_derivedFromCanvas) && (!m_imageElement || !m_der
ivedFromCanvas)); |
| 180 |
| 173 if (m_imageElement) | 181 if (m_imageElement) |
| 174 return m_imageElement->cachedImage()->image(); | 182 return m_imageElement->cachedImage()->image(); |
| 175 return m_bitmap; | 183 return m_bitmap; |
| 176 } | 184 } |
| 177 | 185 |
| 178 } | 186 } |
| OLD | NEW |