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