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