| 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 "core/html/canvas/CanvasRenderingContext.h" | 11 #include "core/html/canvas/CanvasRenderingContext.h" |
| 12 #include "platform/graphics/BitmapImage.h" | 12 #include "platform/graphics/BitmapImage.h" |
| 13 #include "platform/graphics/GraphicsContext.h" | 13 #include "platform/graphics/GraphicsContext.h" |
| 14 #include "platform/graphics/ImageBuffer.h" | 14 #include "platform/graphics/ImageBuffer.h" |
| 15 #include "wtf/RefPtr.h" | 15 #include "wtf/RefPtr.h" |
| 16 | 16 |
| 17 using namespace std; |
| 18 |
| 17 namespace WebCore { | 19 namespace WebCore { |
| 18 | 20 |
| 19 static inline IntRect normalizeRect(const IntRect& rect) | 21 static inline IntRect normalizeRect(const IntRect& rect) |
| 20 { | 22 { |
| 21 return IntRect(std::min(rect.x(), rect.maxX()), | 23 return IntRect(min(rect.x(), rect.maxX()), |
| 22 std::min(rect.y(), rect.maxY()), | 24 min(rect.y(), rect.maxY()), |
| 23 std::max(rect.width(), -rect.width()), | 25 max(rect.width(), -rect.width()), |
| 24 std::max(rect.height(), -rect.height())); | 26 max(rect.height(), -rect.height())); |
| 25 } | 27 } |
| 26 | 28 |
| 27 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) | 29 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) |
| 28 { | 30 { |
| 29 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cro
pRect); | 31 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cro
pRect); |
| 30 if (!intersectRect.width() || !intersectRect.height()) | 32 if (!intersectRect.width() || !intersectRect.height()) |
| 31 return nullptr; | 33 return nullptr; |
| 32 | 34 |
| 33 SkBitmap cropped; | 35 SkBitmap cropped; |
| 34 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, inters
ectRect); | 36 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, inters
ectRect); |
| 35 return BitmapImage::create(NativeImageSkia::create(cropped)); | 37 return BitmapImage::create(NativeImageSkia::create(cropped)); |
| 36 } | 38 } |
| 37 | 39 |
| 38 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | 40 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) |
| 39 : m_imageElement(image) | 41 : m_imageElement(image) |
| 40 , m_bitmap(nullptr) | 42 , m_bitmap(nullptr) |
| 41 , m_cropRect(cropRect) | 43 , m_cropRect(cropRect) |
| 42 { | 44 { |
| 43 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); | 45 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); |
| 44 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | 46 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 45 m_bitmapOffset = srcRect.location(); | 47 m_bitmapOffset = srcRect.location(); |
| 46 | 48 |
| 47 if (!srcRect.width() || !srcRect.height()) | 49 if (!srcRect.width() || !srcRect.height()) |
| 48 m_imageElement = nullptr; | 50 m_imageElement = nullptr; |
| 49 else | 51 else |
| 50 m_imageElement->addClient(this); | 52 m_imageElement->addClient(this); |
| 51 | 53 |
| 52 ScriptWrappable::init(this); | 54 ScriptWrappable::init(this); |
| 53 } | 55 } |
| 54 | 56 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 IntRect dstRect(IntPoint(), srcRect.size()); | 69 IntRect dstRect(IntPoint(), srcRect.size()); |
| 68 | 70 |
| 69 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size()); | 71 OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size()); |
| 70 if (!buf) | 72 if (!buf) |
| 71 return; | 73 return; |
| 72 GraphicsContext* c = buf->context(); | 74 GraphicsContext* c = buf->context(); |
| 73 c->clip(dstRect); | 75 c->clip(dstRect); |
| 74 c->translate(-srcRect.x(), -srcRect.y()); | 76 c->translate(-srcRect.x(), -srcRect.y()); |
| 75 video->paintCurrentFrameInContext(c, videoRect); | 77 video->paintCurrentFrameInContext(c, videoRect); |
| 76 m_bitmap = buf->copyImage(DontCopyBackingStore); | 78 m_bitmap = buf->copyImage(DontCopyBackingStore); |
| 77 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | 79 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 78 | 80 |
| 79 ScriptWrappable::init(this); | 81 ScriptWrappable::init(this); |
| 80 } | 82 } |
| 81 | 83 |
| 82 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) | 84 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) |
| 83 : m_imageElement(nullptr) | 85 : m_imageElement(nullptr) |
| 84 , m_cropRect(cropRect) | 86 , m_cropRect(cropRect) |
| 85 , m_bitmapOffset(IntPoint()) | 87 , m_bitmapOffset(IntPoint()) |
| 86 { | 88 { |
| 87 CanvasRenderingContext* sourceContext = canvas->renderingContext(); | 89 CanvasRenderingContext* sourceContext = canvas->renderingContext(); |
| 88 if (sourceContext && sourceContext->is3d()) | 90 if (sourceContext && sourceContext->is3d()) |
| 89 sourceContext->paintRenderingResultsToCanvas(); | 91 sourceContext->paintRenderingResultsToCanvas(); |
| 90 | 92 |
| 91 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size())
); | 93 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size())
); |
| 92 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | 94 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 93 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cr
opRect); | 95 m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cr
opRect); |
| 94 | 96 |
| 95 ScriptWrappable::init(this); | 97 ScriptWrappable::init(this); |
| 96 } | 98 } |
| 97 | 99 |
| 98 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | 100 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) |
| 99 : m_imageElement(nullptr) | 101 : m_imageElement(nullptr) |
| 100 , m_cropRect(cropRect) | 102 , m_cropRect(cropRect) |
| 101 , m_bitmapOffset(IntPoint()) | 103 , m_bitmapOffset(IntPoint()) |
| 102 { | 104 { |
| 103 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 105 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
| 104 | 106 |
| 105 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size()); | 107 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size()); |
| 106 if (!buf) | 108 if (!buf) |
| 107 return; | 109 return; |
| 108 if (srcRect.width() > 0 && srcRect.height() > 0) | 110 if (srcRect.width() > 0 && srcRect.height() > 0) |
| 109 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, In
tPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y()))); | 111 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, In
tPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); |
| 110 | 112 |
| 111 m_bitmap = buf->copyImage(DontCopyBackingStore); | 113 m_bitmap = buf->copyImage(DontCopyBackingStore); |
| 112 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | 114 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 113 | 115 |
| 114 ScriptWrappable::init(this); | 116 ScriptWrappable::init(this); |
| 115 } | 117 } |
| 116 | 118 |
| 117 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | 119 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) |
| 118 : m_imageElement(bitmap->imageElement()) | 120 : m_imageElement(bitmap->imageElement()) |
| 119 , m_bitmap(nullptr) | 121 , m_bitmap(nullptr) |
| 120 , m_cropRect(cropRect) | 122 , m_cropRect(cropRect) |
| 121 , m_bitmapOffset(IntPoint()) | 123 , m_bitmapOffset(IntPoint()) |
| 122 { | 124 { |
| 123 IntRect oldBitmapRect = bitmap->bitmapRect(); | 125 IntRect oldBitmapRect = bitmap->bitmapRect(); |
| 124 IntRect srcRect = intersection(cropRect, oldBitmapRect); | 126 IntRect srcRect = intersection(cropRect, oldBitmapRect); |
| 125 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()
), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); | 127 m_bitmapRect = IntRect(IntPoint(max(0, oldBitmapRect.x() - cropRect.x()), ma
x(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); |
| 126 | 128 |
| 127 if (m_imageElement) { | 129 if (m_imageElement) { |
| 128 m_imageElement->addClient(this); | 130 m_imageElement->addClient(this); |
| 129 m_bitmapOffset = srcRect.location(); | 131 m_bitmapOffset = srcRect.location(); |
| 130 } else if (bitmap->bitmapImage()) { | 132 } else if (bitmap->bitmapImage()) { |
| 131 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); | 133 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); |
| 132 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); | 134 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); |
| 133 } | 135 } |
| 134 | 136 |
| 135 ScriptWrappable::init(this); | 137 ScriptWrappable::init(this); |
| 136 } | 138 } |
| 137 | 139 |
| 138 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | 140 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) |
| 139 : m_imageElement(nullptr) | 141 : m_imageElement(nullptr) |
| 140 , m_cropRect(cropRect) | 142 , m_cropRect(cropRect) |
| 141 { | 143 { |
| 142 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()))
; | 144 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()))
; |
| 143 m_bitmap = cropImage(image, cropRect); | 145 m_bitmap = cropImage(image, cropRect); |
| 144 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | 146 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y())
), srcRect.size()); |
| 145 | 147 |
| 146 ScriptWrappable::init(this); | 148 ScriptWrappable::init(this); |
| 147 } | 149 } |
| 148 | 150 |
| 149 ImageBitmap::~ImageBitmap() | 151 ImageBitmap::~ImageBitmap() |
| 150 { | 152 { |
| 151 #if !ENABLE(OILPAN) | 153 #if !ENABLE(OILPAN) |
| 152 if (m_imageElement) | 154 if (m_imageElement) |
| 153 m_imageElement->removeClient(this); | 155 m_imageElement->removeClient(this); |
| 154 #endif | 156 #endif |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 return FloatSize(width(), height()); | 231 return FloatSize(width(), height()); |
| 230 } | 232 } |
| 231 | 233 |
| 232 void ImageBitmap::trace(Visitor* visitor) | 234 void ImageBitmap::trace(Visitor* visitor) |
| 233 { | 235 { |
| 234 visitor->trace(m_imageElement); | 236 visitor->trace(m_imageElement); |
| 235 ImageLoaderClient::trace(visitor); | 237 ImageLoaderClient::trace(visitor); |
| 236 } | 238 } |
| 237 | 239 |
| 238 } | 240 } |
| OLD | NEW |