| 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/page/ImageBitmapCallback.h" |
| 13 #include "core/platform/graphics/GraphicsContext.h" | 13 #include "core/platform/graphics/GraphicsContext.h" |
| 14 #include "skia/ext/platform_canvas.h" |
| 15 #include "third_party/skia/include/core/SkColorPriv.h" |
| 16 #include "third_party/skia/include/core/SkSurface.h" |
| 17 #include "third_party/skia/include/gpu/GrContext.h" |
| 18 #include "third_party/skia/include/gpu/SkGpuDevice.h" |
| 14 #include "wtf/RefPtr.h" | 19 #include "wtf/RefPtr.h" |
| 15 | 20 |
| 16 using namespace std; | 21 using namespace std; |
| 17 | 22 |
| 18 namespace WebCore { | 23 namespace WebCore { |
| 19 | 24 |
| 20 static inline IntRect normalizeRect(const IntRect rect) | 25 static inline IntRect normalizeRect(const IntRect rect) |
| 21 { | 26 { |
| 22 return IntRect(min(rect.x(), rect.maxX()), | 27 return IntRect(min(rect.x(), rect.maxX()), |
| 23 min(rect.y(), rect.maxY()), | 28 min(rect.y(), rect.maxY()), |
| 24 max(rect.width(), -rect.width()), | 29 max(rect.width(), -rect.width()), |
| 25 max(rect.height(), -rect.height())); | 30 max(rect.height(), -rect.height())); |
| 26 } | 31 } |
| 27 | 32 |
| 28 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) | 33 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) |
| 29 { | 34 { |
| 30 SkBitmap cropped; | 35 SkBitmap cropped; |
| 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe
ct); | 36 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe
ct); |
| 32 return BitmapImage::create(NativeImageSkia::create(cropped)); | 37 return BitmapImage::create(NativeImageSkia::create(cropped)); |
| 33 } | 38 } |
| 34 | 39 |
| 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) | 40 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) |
| 36 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 41 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 37 , m_size(cropRect.size()) | 42 , m_cropRect(cropRect) |
| 43 , m_canvasImage(0) |
| 38 { | 44 { |
| 39 Image* bitmapImage = image->cachedImage()->image(); | 45 Image* bitmapImage = image->cachedImage()->image(); |
| 40 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 46 m_bitmap = cropImage(bitmapImage, cropRect); |
| 47 m_bitmapSize = IntSize(m_bitmap->size()); |
| 41 | 48 |
| 42 ScriptWrappable::init(this); | 49 ScriptWrappable::init(this); |
| 43 } | 50 } |
| 44 | 51 |
| 45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) | 52 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) |
| 46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 53 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 47 , m_size(cropRect.size()) | 54 , m_cropRect(cropRect) |
| 55 , m_canvasImage(0) |
| 48 { | 56 { |
| 49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); | 57 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); |
| 50 IntRect srcRect = intersection(cropRect, videoRect); | 58 IntRect srcRect = intersection(cropRect, videoRect); |
| 51 IntRect dstRect(IntPoint(), srcRect.size()); | 59 IntRect dstRect(IntPoint(), srcRect.size()); |
| 52 | 60 |
| 53 m_buffer = ImageBuffer::create(videoRect.size()); | 61 m_buffer = ImageBuffer::create(videoRect.size()); |
| 54 GraphicsContext* c = m_buffer->context(); | 62 GraphicsContext* c = m_buffer->context(); |
| 55 c->clip(dstRect); | 63 c->clip(dstRect); |
| 56 c->translate(-srcRect.x(), -srcRect.y()); | 64 c->translate(-srcRect.x(), -srcRect.y()); |
| 57 video->paintCurrentFrameInContext(c, videoRect); | 65 video->paintCurrentFrameInContext(c, videoRect); |
| 58 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor
e).get()); | 66 m_bitmap = m_buffer->copyImage(DontCopyBackingStore); |
| 67 m_bitmapSize = IntSize(m_bitmap->size()); |
| 59 | 68 |
| 60 ScriptWrappable::init(this); | 69 ScriptWrappable::init(this); |
| 61 } | 70 } |
| 62 | 71 |
| 63 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) | 72 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) |
| 64 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 73 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 65 , m_size(cropRect.size()) | 74 , m_cropRect(cropRect) |
| 75 , m_bitmapSize(cropRect.size()) |
| 66 { | 76 { |
| 67 IntSize canvasSize = canvas->size(); | 77 m_canvasImage = canvas->buffer()->imageSnapshot(); |
| 68 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); | |
| 69 IntRect dstRect(IntPoint(), srcRect.size()); | |
| 70 | |
| 71 m_buffer = ImageBuffer::create(canvasSize); | |
| 72 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); | |
| 73 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor
e).get()); | |
| 74 | 78 |
| 75 ScriptWrappable::init(this); | 79 ScriptWrappable::init(this); |
| 76 } | 80 } |
| 77 | 81 |
| 78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) | 82 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) |
| 79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) | 83 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) |
| 80 , m_size(cropRect.size()) | 84 , m_cropRect(cropRect) |
| 85 , m_canvasImage(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_bitmapSize = IntSize(m_bitmap->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, IntRect cropRect) |
| 94 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b
itmap->bitmapOffset().y() - cropRect.y())) | 100 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b
itmap->bitmapOffset().y() - cropRect.y())) |
| 95 , m_size(cropRect.size()) | 101 , m_cropRect(cropRect) |
| 102 , m_canvasImage(0) |
| 96 { | 103 { |
| 97 Image* bitmapImage = bitmap->bitmapImage(); | 104 Image* bitmapImage = bitmap->bitmapImage().get(); |
| 98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset(
).y())); | 105 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset(
).y())); |
| 99 m_bitmap = cropImage(bitmapImage, cropRect).get(); | 106 m_bitmap = cropImage(bitmapImage, cropRect); |
| 107 m_bitmapSize = IntSize(m_bitmap->size()); |
| 100 | 108 |
| 101 ScriptWrappable::init(this); | 109 ScriptWrappable::init(this); |
| 102 } | 110 } |
| 103 | 111 |
| 104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro
pRect) | 112 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro
pRect) |
| 105 { | 113 { |
| 106 IntRect normalizedCropRect = normalizeRect(cropRect); | 114 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr
opRect))); | 115 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr
opRect))); |
| 108 return imageBitmap.release(); | 116 return imageBitmap.release(); |
| 109 } | 117 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 130 } | 138 } |
| 131 | 139 |
| 132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec
t) | 140 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec
t) |
| 133 { | 141 { |
| 134 IntRect normalizedCropRect = normalizeRect(cropRect); | 142 IntRect normalizedCropRect = normalizeRect(cropRect); |
| 135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC
ropRect))); | 143 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC
ropRect))); |
| 136 return imageBitmap.release(); | 144 return imageBitmap.release(); |
| 137 } | 145 } |
| 138 | 146 |
| 139 } | 147 } |
| OLD | NEW |